Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 下载大文件不起作用_Php_Download - Fatal编程技术网

Php 下载大文件不起作用

Php 下载大文件不起作用,php,download,Php,Download,嘿,我有下面的代码来下载一个大文件,但是每次下载都会停止,没有完成下载 function download($file) { include('logger.php5'); $log = new Logging(); $log->lfile('download.log'); ini_set('max_execution_time', 86400); //header('Location: '.$file); $filesize = files

嘿,我有下面的代码来下载一个大文件,但是每次下载都会停止,没有完成下载

function download($file)
{
    include('logger.php5');
    $log = new Logging();
    $log->lfile('download.log');
    ini_set('max_execution_time', 86400);
    //header('Location: '.$file);
    $filesize = filesize($file);
    $filename = pathinfo($file, PATHINFO_BASENAME);
    $filext = pathinfo($file, PATHINFO_EXTENSION);
    $mime = include('mime.php5');

    $log->lwrite(ini_get('max_execution_time'));
    $log->lwrite(sprintf('%s %s %s %s', $filename, $filext, $mime[$filext], human_filesize($filesize)));
    $log->lclose();
    @ob_end_clean();
    session_write_close();
    header("Content-Description: File Transfer");
    header("Content-Type: ".$mime[$filext]);
    header("Content-Disposition: ".
     (!strpos($HTTP_USER_AGENT,"MSIE 5.5")?"attachment; ":"").
     "filename=".$filename);
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$filesize);
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header('Pragma: public');
    header('Expires: 0');
    $done = readfile_chunked($file);
}

function readfile_chunked($filename,$retbytes=true) { 
   $chunksize = 1*(1024*1024); // how many bytes per chunk 
   $buffer = ''; 
   $cnt =0; 
   // $handle = fopen($filename, 'rb'); 
   $handle = fopen($filename, 'rb'); 
   if ($handle === false) { 
       return false; 
   } 
   while (!feof($handle)) { 
       $buffer = fread($handle, $chunksize); 
       echo $buffer; 
       ob_flush(); 
       flush(); 
       if ($retbytes) { 
           $cnt += strlen($buffer); 
       } 
   } 
       $status = fclose($handle); 
   if ($retbytes && $status) { 
       return $cnt; // return num. bytes delivered like readfile() does. 
   } 
   return $status;
} 
每次我调用脚本时,下载启动但在400MB后停止,文件本身就是778MB

有人能看到代码有问题吗

更新


尝试记录
readfile\u chunked
的返回值后,感觉脚本停止了,而不是下载本身。因为在
readfile\u chunked
调用之后,我无法获取日志条目。

这可能是PHP中的
filesize
函数有问题。对于大文件大小的读取,存在已知的bug,当您将其作为文件头发送时,我建议您在不使用以下行的情况下尝试脚本:

 header("Content-Length: ".$filesize);
header("Content-Transfer-Encoding: binary");
哦,也许你可以看看这句话:

 header("Content-Length: ".$filesize);
header("Content-Transfer-Encoding: binary");
我认为应该检查每个文件的编码。像这样:

$finfo = finfo_open(FILEINFO_MIME);

//check to see if the mime-type starts with 'text'
return substr(finfo_file($finfo, $filename), 0, 4) == 'text';

若它是一个文本文件,你们当然应该使用ASCII。与这个问题无关,但我认为这是对脚本的一个有用的补充:)

所有标题都设置在
下载
函数内部,外部只是用于识别正确文件的安全工具。是的,正确的标题会被发送。当你说下载停止时,你的意思是客户端只收到400MB吗?还是说
readfile\u chunked
函数返回400MB的计数?或者两者都有?客户端获得了778MB的头文件,但在大约400MB之后,脚本似乎就停止了。我试图在
readfile\u chunked
之后写一行日志,但没有执行。因此运行时很可能正在终止脚本。有什么记录吗?