当前位置:Gxlcms > PHP教程 > php支持多线程下载的例子

php支持多线程下载的例子

时间:2021-07-01 10:21:17 帮助过:7人阅读

  1. header("Cache-Control: public");

  2. header("Accept-Ranges: bytes");

  3. $file = "a.7z";

  4. $filename = "a.7z";

  5. $size=filesize($file);

  6. $size1=$size-1;
  7. //获得字节范围
  8. if(isset($_SERVER['HTTP_RANGE'])) {
  9. list($name, $range) = explode("=",$_SERVER['HTTP_RANGE']);
  10. $length=$size1-$range;
  11. header("HTTP/1.1 206 Partial Content"); //http协议头状态码,表示以部分内容传输
  12. header("Content-Range: bytes ".$range."-".$size1."/".$size);
  13. }else{
  14. $length = $size;
  15. }

  16. header("Content-Length: $length");

  17. header("Content-Type: application/octet-stream");
  18. header("Content-Disposition: attachment; filename=".$filename);

  19. $fp=fopen($file,"rb");

  20. //设定文件指针位置

  21. fseek($fp,$range);

  22. while(!feof($fp)){

  23. set_time_limit(0);
  24. echo fread($fp,1024);
  25. flush();
  26. ob_flush();
  27. }
  28. fclose($fp);
  29. exit;
  30. ?>

人气教程排行