简体   繁体   中英

File download PHP script doesn't work because of server delay

I have this code to download a file, but on sourceforge.net sever there is a 5 seconds delay before file starts to download (You can see it if you try to load this link in browser). And I have file with zero size after script is done. How can I download this file? Thanx in advance!

$url = 'http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe';
$ch = curl_init($url);
$fp = fopen('/home/content/11/8564211/html/'.substr($url,strrpos($url,'/'),strlen($url)), 'w'); 
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Sourceforge uses a meta refresh tag to start the download and because CURLOPT_FOLLOWLOCATION responds to Location: header it will most likely not help.

I think you're going to have to do som HTML parsing to achieve what you want to do. You have to find this line:

<meta http-equiv="refresh" content="5; url=http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe?r=&amp;ts=1333621946&amp;use_mirror=switch">

Then you must get the url from the line and load that. It's possible that Sourceforge uses some cookie or session based stopper for this kind of downloads so you may have to compensate for that.

I haven't tested this but it looks like this is close to the way you have to do this.

To avoid timeouts in PHP you can use:

set_time_limit($hugetimeout);

before your script. You can read further documentation here .

You can try this:

$url = 'http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe';
$ch = curl_init($url);
$fp = fopen('/home/content/11/8564211/html/'.substr($url,strrpos($url,'/'),strlen($url)), 'w'); 
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);

On the download page, there's a direct link, you could try using that instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM