在php下载大文件时网站冻结

时间:2014-07-26 12:45:59

标签: php wordpress download

我正在使用php下载大.mp4个文件。我正在使用下载按钮进行下载,但是当我点击下载按钮时,网站冻结,我无法移动到其他地方。我在互联网上搜索了几乎所有的东西,以避免这种情况,但我不能。我找不到解决方案。希望您能够帮助我。提前谢谢。

以下是下载代码

  function download($file){

$ext = explode(".", $file);
switch($ext[sizeof($ext)-1])
{
  case "mp4": $mime = "video/mp4"; break;
  case "m4a": $mime = "audio/mp4"; break;
  case "m4p": $mime = "audio/mp4"; break;
 }

header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

    $ch = curl_init($file);

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_HEADER, TRUE);
 curl_setopt($ch, CURLOPT_NOBODY, TRUE);

 $data = curl_exec($ch);
 $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

 curl_close($ch);
header("Content-Description: File Transfer");
header('Content-Length: '. $size);
ob_clean();
ob_end_clean() ;

readfile($file);
 exit();

}


if(isset($_POST['indir'])) { 
$file=...
download($file);
}

1 个答案:

答案 0 :(得分:0)

有时readfile()会在处理大文件时导致问题(即使PHP文档另有说明)

您可以尝试以块的形式阅读:

$fd = fopen ($fullPath, "r")
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
}
fclose ($fd);