-
<?php
-
-
$url="";
-
-
$ch=curl_init();//Initialize a cURL session.
-
-
$a_opt=array(
-
CURLOPT_URL => $url,//The URL to fetch. This can also be set when initializing a session with curl_init().
-
CURLOPT_HEADER => 0,//FALSE to not include the header in the output,TRUE to include the header in the output.
-
CURLOPT_RETURNTRANSFER => 1,//TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 当CURLOPT_RETURNTRANSFER设置为false时,方法curl_exec的调用在成功时会直接输出结果并返回bool(true),而不是以字符串形式返回取得的结果数据.
-
CURLOPT_NOPROGRESS => 0,//TRUE to disable the progress meter for cURL transfers. Note: PHP automatically sets this option to TRUE, this should only be changed for debugging purposes. 设置为false时会显示进度条(进度条显示在错误日志里,即apache的log目录下error.log里,不在brower中显示),注意:php默认设置为true,应该仅在调试时置为false.
-
CURLOPT_HEADERFUNCTION => 'read_header', //The name of a callback function where the callback function takes two parameters. The first is the cURL resource, the second is a string with the header data to be written. The header data must be written when using this callback function. Return the number of bytes written.
-
CURLOPT_WRITEFUNCTION => 'read_body',//The name of a callback function where the callback function takes two parameters. The first is the cURL resource, and the second is a string with the data to be written. The data must be saved by using this callback function. It must return the exact number of bytes written or the transfer will be aborted with an error.
-
);
-
-
curl_setopt_array($ch,$a_opt);//Sets multiple options for a cURL session. This function is useful for setting a large amount of cURL options without repetitively calling curl_setopt().
-
-
$percent = "0%";
-
$download_filelength = 0;
-
$download_filename = "download.mp3";//default download file name.
-
$fheader = fopen('/var/www/download.txt','w');//default save path for header info
-
$fbody = fopen("/var/www/".$download_filename,'w');
-
-
//Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.成功时返回true,失败时返回false;若CURLOPT_RETURNTRANSFER设置为true,则成功时返回取得的结果,失败时返回false.
-
$str=curl_exec($ch);
-
-
curl_close($ch);
-
fclose($fheader);
-
fclose($fbody);
-
-
-
function read_header($ch,$string){
-
global $download_filelength;
-
global $download_filename;
-
global $fheader;
-
global $fbody;
-
//从头信息中抓取下载文件的长度大小
-
if(preg_match("/Content-Length: ([0-9]+)/i",$string,$matches)){
-
$download_filelength = intval($matches[1]);
-
echo "file length is : $download_filelength"."
";
-
}else{
-
echo "not found the file length from the header info"."
";
-
}
-
//从头信息中抓取下载文件的文件名
-
if(preg_match("/filename=\"(.+)\"/i",$string,$matches)){
-
-
$download_filename = mb_convert_encoding($matches[1],"utf-8","GBK");//文件名转碼
-
-
$fbody = fopen("/var/www/".$download_filename,'w');
-
-
echo "file name is : $download_filename"."
";
-
}else{
-
echo "not found the file name from the header info"."
";
-
}
-
-
fwrite($fheader,$string);
-
-
return strlen($string);
-
}
-
-
-
function read_body($ch,$string){
-
global $fbody;
-
global $download_filename;
-
-
//输出已下载大小
-
echo "Downloaded : ".system('du -b ' . $download_filename . ' | awk \'{print $1}\'');
-
-
fwrite($fbody,$string);
-
-
return strlen($string);
-
}
-
-
?>
新建以上文件于apache更目录,修改url为实际下载文件的remote url,然后从浏览器访问该php文件,则可在/var/log/apache2/下的error.log中看到文件实时下载进度(tail -f error.log跟踪日志即可)
阅读(3817) | 评论(0) | 转发(0) |