开发者

php curl download file in download manager

i have a problem with a file output, it is really small, like around 4kb and file said "400 - Bad Request".

from开发者_Go百科 source, the correct size is 28.2mb.

$url = 'http://mozilla.isu.net.sa/firefox/releases/7.0.1/mac/en-US/Firefox%207.0.1.dmg';
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); 
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 360);
curl_setopt($curl, CURLOPT_COOKIEJAR, 'temp/cookie.txt'); 
curl_setopt($curl, CURLOPT_COOKIEFILE, 'temp/cookie.txt');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');

$curl_out = curl_exec($curl);

curl_close($curl);

$filename = explode('/', $url);
$filename = $filename[count($filename)-1];

header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.$filename.'"');

print($curl_out);

If It cant handle bigger file, other method: i want to download from php with cookie that forward to source link... then how?


Not sure what you are trying to do, but from what i see, you are downloading the file then offering the user to download it again, so for each request to this link you will have to download it again. So there is too much overhead.

Here is a simpler solution:

<a href="http://mozilla.isu.net.sa/firefox/releases/7.0.1/mac/en-US/Firefox%207.0.1.dmg">Click here to download</a>


Download the file using your browser,observe, copy and replicate all the headers and cookies (use tamper data(firefox) to see the headers)

and for downloading a file, i will suggest you to try something like this along with what you have already done.

$url  = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';

$fp = fopen($path, 'w');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);

$data = curl_exec($ch);

curl_close($ch);
fclose($fp);

Apart from that, i tried your link and download starts directly, so the code above should be sufficient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜