如何通过PHP脚本下载mp4视频时提高下载速度

时间:2011-04-16 12:03:49

标签: php download

我正在使用脚本下载视频,但下载需要很长时间。是否有任何流程或其他脚本可以帮助我?

// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

// download
// @readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {

    print(fread($file, 1024*100));
    flush();
    if (connection_status()!=0) {
      @fclose($file);
      die();
    }
  }
  @fclose($file);
}

2 个答案:

答案 0 :(得分:1)

使用readfile()函数(如您原来的那样)将允许您直接从文件换行到输出,而不是使用分块循环和打印。那你为什么选择做这个块循环?

答案 1 :(得分:0)

如上所述,readfile()是一种方式。

另一种更优选的方法取决于您的网络服务器。 NginX,Lighttpd以及Apache的模块,允许您将带有文件路径/名称的头传递给服务器,它将直接从服务器本身发送文件,因此不需要使用PHP资源来执行此操作。如果那不可能,那么readfile()是你可能拥有的最好的 - 如果你不能只是给某人一个直接的URL来下载它。