Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322704
  • 博文数量: 96
  • 博客积分: 230
  • 博客等级: 二等列兵
  • 技术积分: 722
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-13 22:25
个人简介

心安处即吾乡!

文章分类

全部博文(96)

文章存档

2016年(1)

2014年(79)

2013年(7)

2012年(9)

我的朋友

分类: PHP

2012-04-18 19:13:56


点击(此处)折叠或打开

  1. <?php

  2. $url="";

  3. $ch=curl_init();//Initialize a cURL session.

  4. $a_opt=array(
  5.       CURLOPT_URL => $url,//The URL to fetch. This can also be set when initializing a session with curl_init().
  6.       CURLOPT_HEADER => 0,//FALSE to not include the header in the output,TRUE to include the header in the output.
  7.       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),而不是以字符串形式返回取得的结果数据.
  8.       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.
  9.       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.
  10.       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.
  11.     );
  12.    
  13. 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().

  14. $percent = "0%";
  15. $download_filelength = 0;
  16. $download_filename = "download.mp3";//default download file name.
  17. $fheader = fopen('/var/www/download.txt','w');//default save path for header info
  18. $fbody = fopen("/var/www/".$download_filename,'w');

  19. //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.
  20. $str=curl_exec($ch);

  21. curl_close($ch);
  22. fclose($fheader);
  23. fclose($fbody);


  24. function read_header($ch,$string){
  25. global $download_filelength;
  26. global $download_filename;
  27. global $fheader;
  28. global $fbody;
  29. //从头信息中抓取下载文件的长度大小
  30. if(preg_match("/Content-Length: ([0-9]+)/i",$string,$matches)){
  31. $download_filelength = intval($matches[1]);
  32. echo "file length is : $download_filelength"."
    "
    ;
  33. }else{
  34. echo "not found the file length from the header info"."
    "
    ;
  35. }
  36.   //从头信息中抓取下载文件的文件名
  37.   if(preg_match("/filename=\"(.+)\"/i",$string,$matches)){
  38.  
  39.   $download_filename = mb_convert_encoding($matches[1],"utf-8","GBK");//文件名转碼
  40.  
  41.   $fbody = fopen("/var/www/".$download_filename,'w');
  42.  
  43.   echo "file name is : $download_filename"."
    "
    ;
  44.   }else{
  45.   echo "not found the file name from the header info"."
    "
    ;
  46.   }
  47.  
  48.   fwrite($fheader,$string);
  49.  
  50.   return strlen($string);
  51. }


  52. function read_body($ch,$string){
  53. global $fbody;
  54. global $download_filename;

  55. //输出已下载大小
  56. echo "Downloaded : ".system('du -b ' . $download_filename . ' | awk \'{print $1}\'');

  57.   fwrite($fbody,$string);

  58.   return strlen($string);
  59. }

  60. ?>

新建以上文件于apache更目录,修改url为实际下载文件的remote url,然后从浏览器访问该php文件,则可在/var/log/apache2/下的error.log中看到文件实时下载进度(tail -f error.log跟踪日志即可)


阅读(3825) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~