已下载文件但尺寸不同

时间:2015-12-12 07:25:37

标签: php curl

我有一个cURL脚本来下载文件,但下载的文件大小与服务器大小不同:

下载窗口弹出消息:

Document (389 bytes)

但该文件是MP3并给出了文档。

在我的计算机上,当我检查文件信息时,会给出:

Kind: MP3 audio 
Size: 389 bytes (4 KB on disk)

代码是

$url = strip_tags($_GET['path']);

//echo $url.'<br>';

$fileName = strip_tags($_GET['file']);

//echo $fileName;

set_time_limit(0);

header("Pragma: public");
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false);
header("Content-Type: application/download"); 
header("Content-Disposition: filename=$fileName");


set_time_limit(0);
$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');//This is the file where we save the    information
$ch = curl_init(str_replace(" ","%20",$url.$fileName));//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);

检查服务器上文件大小的命令行:

419036 Mar 24  2014 001.mp3

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

这对我有用。还要确保www用户有足够的权限在目录上写入。您使用了错误的HTTP囤积者来卷曲。这就是下载失败的原因。

<?php

$url = strip_tags($_GET['path']);

//echo $url.'<br>';

$fileName = strip_tags($_GET['file']);

//echo $fileName;

set_time_limit(0);

$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');//This is the file where we save the    information
$url = str_replace(" ","%20",$url.$fileName);

$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Host   www.alfirdaous.com',
    'User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:42.0) Gecko/20100101 Firefox/42.0',
    'Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language    en-US,en;q=0.5',
    'Accept-Encoding    gzip, deflate',
    'Connection keep-alive',
));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);

?>