jso*_*onx 16 php header file download
我正在使用简单的文件下载脚本:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
Run Code Online (Sandbox Code Playgroud)
它正在我的本地服务器上工作高达200mb.
当我在我的网站上尝试此代码时,它下载173KB而不是200MB文件.
我检查了一切,写了一些自定义代码(使用ob函数和fread而不是readfile)但无法下载大文件.
谢谢您的回答.
Rob*_*itt 19
我对以下代码的一个问题是你无法控制输出流,你让PHP处理它而不知道背景中究竟发生了什么:
您应该做的是设置一个输出系统,您可以控制和复制accros服务器.
例如:
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked'); //changed to chunked
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//header('Content-Length: ' . filesize($file)); //Remove
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
Run Code Online (Sandbox Code Playgroud)
这只是基本但是试一试!
另请阅读我的回复:file_get_contents => PHP致命错误:允许的内存耗尽