gpt4 book ai didi

php, 文件下载

转载 作者:IT王子 更新时间:2023-10-29 00:17:11 31 4
gpt4 key购买 nike

我正在使用简单的文件下载脚本:

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;
}

它在我的本地服务器上运行高达 200mb。

当我在我的网站上尝试此代码时,它会下载 173KB 而不是 200MB 的文件。

我检查了所有内容,编写了一些自定义代码(使用 ob 函数和 fread 而不是 readfile)但无法下载大文件。

感谢您的回答。

  • 我正在使用 Apache 2.2、PHP 5.3
  • 处理大文件的所有 PHP 设置都可以。 (执行时间、内存限制、...

最佳答案

我遇到的以下代码的一个问题是您无法控制输出流,您让 PHP 在不知道后台到底发生了什么的情况下处理它:

你应该做的是建立一个你可以控制的输出系统并复制到服务器上。

例如:

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>";

这只是基本操作,但请尝试一下!

另请阅读我的回复:file_get_contents => PHP Fatal error: Allowed memory exhausted

关于php, 文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5595485/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com