php断点下载

Java代码   收藏代码
  1. <?php  
  2. /** 
  3.  
  4.  * PHP-HTTP断点续传实现 
  5.  
  6.  * @param string $path: 文件所在路径 
  7.  
  8.  * @param string $file: 文件名 
  9.  
  10.  * @return void 
  11.  
  12.  */  
  13.   
  14. function download($path, $file) {  
  15.       
  16.     $real = $path . '/' . $file;  
  17.       
  18.     if (! file_exists ( $real )) {  
  19.           
  20.         return false;  
  21.       
  22.     }  
  23.       
  24.     $size = filesize ( $real );  
  25.       
  26.     $size2 = $size - 1;  
  27.       
  28.     $range = 0;  
  29.       
  30.     if (isset ( $_SERVER ['HTTP_RANGE'] )) {  
  31.           
  32.         header ( 'HTTP /1.1 206 Partial Content' );  
  33.           
  34.         $range = str_replace ( '=''-', $_SERVER ['HTTP_RANGE'] );  
  35.           
  36.         $range = explode ( '-', $range );  
  37.           
  38.         $range = trim ( $range [1] );  
  39.           
  40.         header ( 'Content-Length:' . $size );  
  41.           
  42.         header ( 'Content-Range: bytes ' . $range . '-' . $size2 . '/' . $size );  
  43.       
  44.     } else {  
  45.           
  46.         header ( 'Content-Length:' . $size );  
  47.           
  48.         header ( 'Content-Range: bytes 0-' . $size2 . '/' . $size );  
  49.       
  50.     }  
  51.       
  52.     header ( 'Accenpt-Ranges: bytes' );  
  53.       
  54.     header ( 'application/octet-stream' );  
  55.       
  56.     header ( "Cache-control: public" );  
  57.       
  58.     header ( "Pragma: public" );  
  59.       
  60.     //解决在IE中下载时中文乱码问题  
  61.       
  62.   
  63.     $ua = $_SERVER ['HTTP_USER_AGENT'];  
  64.       
  65.     if (preg_match ( '/MSIE/', $ua )) {  
  66.           
  67.         $ie_filename = str_replace ( '+''%20', urlencode ( $file ) );  
  68.           
  69.         header ( 'Content-Dispositon:attachment; filename=' . $ie_filename );  
  70.       
  71.     } else {  
  72.           
  73.         header ( 'Content-Dispositon:attachment; filename=' . $file );  
  74.       
  75.     }  
  76.       
  77.     $fp = fopen ( $real, 'rb+' );  
  78.       
  79.     fseek ( $fp, $range );  
  80.       
  81.     while ( ! feof ( $fp ) ) {  
  82.         set_time_limit ( 0 );  
  83.           
  84.         print (fread ( $fp, 1024 )) ;  
  85.           
  86.         flush ();  
  87.           
  88.         ob_flush ();  
  89.       
  90.     }  
  91.       
  92.     fclose ( $fp );  
  93.   
  94. }  
  95. /*End of PHP*/  
 
Java代码   收藏代码
  1. ob_start();  
  2. $size = filesize($file_dir . $file_name);  
  3. // 输入文件标签  
  4. Header("Content-type: application/octet-stream");  
  5. Header("Cache-Control: must-revalidate, post-check=0, pre-check=0");              
  6. Header("Content-length: " . $size);  
  7.   
  8. $encoded_filename = urlencode($down_name);  
  9. $encoded_filename = str_replace("+""%20", $encoded_filename);  
  10.   
  11. $ua = $_SERVER["HTTP_USER_AGENT"];  
  12. // 处理下载的时候的文件名  
  13. if (preg_match("/MSIE/", $ua)) {  
  14.     header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');  
  15. else if (preg_match("/Firefox/", $ua)) {  
  16.     header('Content-Disposition: attachment; filename*="utf8\'\'' . $down_name . '"');  
  17. else {  
  18.     header('Content-Disposition: attachment; filename="' . $down_name . '"');  
  19. }  
  20. $fp = fopen($file_dir . $file_name, "r"); // 打开文件  
  21.   
  22. if (isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9]+)-/i", $_SERVER['HTTP_RANGE'], $match) && ($match[1] < $size)) {  
  23.     $range = $match[1];                  
  24.     fseek($fp, $range);  
  25.     header("HTTP/1.1 206 Partial Content");  
  26.     //header("Date: " . gmdate("D, d M Y H:i:s") . " GMT");  
  27.     header("Last-Modified: " . gmdate("D, d M Y H:i:s", filemtime($file_dir . $file_name)) . " GMT");  
  28.     header("Accept-Ranges: bytes");  
  29.     $rangesize = ($size - $range) > 0 ? ($size - $range) : 0;  
  30.     header("Content-Length:" . $rangesize);  
  31.     header("Content-Range: bytes " . $range . '-' . ($size - 1) . "/" . $size);  
  32.     // header("Connection: close" . "\n\n" );  
  33.     //header("Connection: close"." ");  
  34. else {                  
  35.     header("Content-Length: " . (string) ($size));  
  36.     header("Accept-Ranges: bytes");  
  37.     $range = 0;  
  38.     header("Content-Range: bytes " . $range . '-' . ($size - 1) . "/" . $size);  
  39. }  
  40. fpassthru($fp);  
  41. ob_end_flush();  
  42. fclose($fp);  
  43. exit;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值