Portal Home > Knowledgebase > Articles Database > PHP exec command doubt


PHP exec command doubt




Posted by sarsh11, 11-08-2010, 12:26 AM
I am not a experienced coder or anything but I just came across a problem while creating my script and would appreciate your help. I am currently working on a script which downloads a video from a website and to do this I use the PHP exec command and execute the command like below: exec("wget -O $name "$link") Now the problem is that this page when accessed via the browser will not fully load until the download is complete, so I was wondering if it was possible to somehow load the whole page while the download is in progress. Maybe by putting the visual aspects in another page and then somehow executing that php file from within this. I came across include function, but I am unsure if that'll do the trick since from what I read it simply inserts the code from that into the page therefore still causing the page to hang untill the download script is completely executed. Any suggestions? Sorry for being vague, if you need me to be more precise please say so.

Posted by kjsrs, 11-08-2010, 12:37 AM
If I understand correctly, you will want to load a different page, then use Ajax (I recommend jQuery) to pull that PHP script. That way, your main page will load, then the browser will call the script using Ajax and load that section of the page when it's available.

Posted by sarsh11, 11-08-2010, 01:28 AM
Ah, I thought it might require some JavaScript, guess I need to find someone to do that now I am currently using an iFrame for this part, but the iFrame itself contains more information such as estimated time etc which isn't seen untill the whole process is complete. I'll keep looking for other methods anyway Thanks kjsrs

Posted by HostForMe, 11-13-2010, 01:20 PM
Hello sarsh, If you haven't found a solution to your problem yet, it's really simple. kjsrs was right, you do need to use Ajax to get this to work properly. However, the implementation is not as hard as you might think. You should separate the download script into its own file so that you have 2 files. Let's say that script.php is the file which contains the download script and exec() command that you have specified in your post; and download.php is the file that takes too long to load and with which you attempted to use the include() function. Follow the following steps: 1. Include this JavaScript code anywhere on download.php Important: Change "script.php" (line 5) in the code to whatever you named your download script file. 2. Now add an onload attribute to the body tag of download.php as indicated by the code below. 3. Ensure that script.php contains nothing but the code required to execute your exec() command. And then everything should work properly. If you have any questions, I'll be happy to further help you. Thank you, HostFor.Me

Posted by zasdf, 11-13-2010, 02:33 PM
If you don't want to use Javascript, you can try to fork it into the background from exec() like so:

Posted by sarsh11, 11-14-2010, 10:13 AM
Okay I am gonna give both of these a try, thanks for taking the time guys..I'll report back with the results. Edit: Okay tried zasdf's meethod, didn't work..still waited till the command was completely executed. Now for HostForMe's method

Posted by DennisTT, 11-14-2010, 06:01 PM
Another possibility: The & normally puts commands into the background...

Posted by Peter Kelly, 11-15-2010, 03:46 AM
Most web hosting providers should have disabled commands such as exec as they can be exploited put to bad use.

Posted by Crandion, 11-16-2010, 01:36 AM
Instead of running wget with exec, I'd rather use PHP's cURL library to do he same thing, but much more elegantly: http://php.net/manual/en/book.curl.php

Posted by funkywizard, 11-18-2010, 04:35 PM
you could try this: that way, something will be posted to the browser before the file starts downloading (so the browser doesn't time out the request), and you set the time limit to 0 so that php won't time out either.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
BigButtVPS is Down ? (Views: 612)
OVH Reseller (Views: 628)