当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP实现下载断点续传的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-25 14:26:06 浏览: 评论:0 

这篇文章主要介绍了PHP实现下载断点续传的方法,通过自定义函数来实现PHP的断点续传下载方法,涉及文件的常见操作与指针和缓冲的用法,代码中备有较为详尽的注释便于阅读和理解,需要的朋友可以参考下。

本文实例讲述了PHP实现下载断点续传的方法。分享给大家供大家参考。

具体实现代码如下:

  1. <?php 
  2. /* 
  3.  * PHP下载断点续传 
  4.  */ 
  5. function dl_file_resume($file){ 
  6.  
  7.     //检测文件是否存在 
  8.     if (!is_file($file)) { die("<b>404 File not found!</b>"); } 
  9.       
  10.     $len = filesize($file);//获取文件大小 
  11.     $filename = basename($file);//获取文件名字 
  12.     $file_extension = strtolower(substr(strrchr($filename,"."),1));//获取文件扩展名 
  13.       
  14.     //根据扩展名 指出输出浏览器格式 
  15.     switch$file_extension ) { 
  16.         case "exe"$ctype="application/octet-stream"break
  17.         case "zip"$ctype="application/zip"break
  18.         case "mp3"$ctype="audio/mpeg"break
  19.         case "mpg":$ctype="video/mpeg"break
  20.         case "avi"$ctype="video/x-msvideo"break
  21.         default$ctype="application/force-download"
  22.     } 
  23.       
  24.     //Begin writing headers 
  25.     header("Cache-Control:"); 
  26.     header("Cache-Control: public"); 
  27.       
  28.     //设置输出浏览器格式 
  29.     header("Content-Type: $ctype"); 
  30.     if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {//如果是IE浏览器 
  31.         # workaround for IE filename bug with multiple periods / multiple dots in filename 
  32.         # that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe 
  33.         $iefilename = preg_replace('/\./''%2e'$filename, substr_count($filename'.') - 1); 
  34.         header("Content-Disposition: attachment; filename=\"$iefilename\""); 
  35.     } else { 
  36.         header("Content-Disposition: attachment; filename=\"$filename\""); 
  37.     } 
  38.     header("Accept-Ranges: bytes"); 
  39.       
  40.     $size=filesize($file); 
  41.     //如果有$_SERVER['HTTP_RANGE']参数 
  42.     if(isset($_SERVER['HTTP_RANGE'])) { 
  43. /*   --------------------------- 
  44.    Range头域   Range头域可以请求实体的一个或者多个子范围。例如,   表示头500个字节:bytes=0-499   表示第二个500字节:bytes=500-999   表示最后500个字节:bytes=-500   表示500字节以后的范围:bytes=500-   第一个和最后一个字节:bytes=0-0,-1   同时指定几个范围:bytes=500-600,601-999   但是服务器可以忽略此请求头,如果无条件GET包含Range请求头,响应会以状态码206(PartialContent)返回而不是以200 (OK)。 
  45.    ---------------------------*/ 
  46.     
  47. // 断点后再次连接 $_SERVER['HTTP_RANGE'] 的值 bytes=4390912- 
  48.     
  49.         list($a$range)=explode("=",$_SERVER['HTTP_RANGE']); 
  50.    //if yes, download missing part 
  51.         str_replace($range"-"$range);//这句干什么的呢。。。。 
  52.    $size2=$size-1;//文件总字节数 
  53.         $new_length=$size2-$range;//获取下次下载的长度 
  54.         header("HTTP/1.1 206 Partial Content"); 
  55.         header("Content-Length: $new_length");//输入总长 
  56.         header("Content-Range: bytes $range$size2/$size");//Content-Range: bytes 4908618-4988927/4988928   95%的时候 
  57.     } else {//第一次连接 
  58.         $size2=$size-1; 
  59.         header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928 
  60.         header("Content-Length: ".$size);//输出总长 
  61.     } 
  62.     //打开文件 
  63.     $fp=fopen("$file","rb"); 
  64.     //设置指针位置 
  65.     fseek($fp,$range); 
  66.     //虚幻输出 
  67.     while(!feof($fp)){ 
  68.         //设置文件最长执行时间 
  69.         set_time_limit(0); 
  70.         print(fread($fp,1024*8));//输出文件 
  71.         flush();//输出缓冲 
  72.         ob_flush(); 
  73.     } 
  74.     fclose($fp); 
  75.     exit
  76.  
  77. dl_file_resume("1.zip");//同级目录的1.zip 文件 
  78.  
  79. //--------------------------------------- 
  80.  
  81. //不支持断点续传的文件下载。 
  82.  
  83. //--------------------------------------- 
  84.  
  85. downFile("1.zip"); 
  86.  
  87. function downFile($sFilePath
  88.    if(file_exists($sFilePath)){ 
  89.        $aFilePath=explode("/",str_replace("\\","/",$sFilePath),$sFilePath); 
  90.        $sFileName=$aFilePath[count($aFilePath)-1]; 
  91.        $nFileSize=filesize ($sFilePath); 
  92.        header ("Content-Disposition: attachment; filename=" . $sFileName); 
  93.        header ("Content-Length: " . $nFileSize); 
  94.        header ("Content-type: application/octet-stream"); 
  95.        readfile($sFilePath); 
  96.    }//www.phpfensi.com 
  97.    else 
  98.    { 
  99.        echo("文件不存在!"); 
  100.    } 
  101. ?> 

希望本文所述对大家的PHP程序设计有所帮助。

Tags: PHP下载断点续传

分享到: