PHP 发送 http 响应 GZIP 大文件

PHP send http response GZIP large file

我正在尝试将简单的 MOCK http 响应设置为 PHP 脚本。 因此我有这个代码:

<?php
$filename = "output.txt.gz";
$filesize = filesize($filename);
//ignore the next 3 lines
$handle = fopen($filename, "r");
$contents = fread($handle, $filesize);
fclose($handle);
header("HTTP/1.1 200 OK");
header("Content-Encoding: gzip");
header("Content-Type: text/xml;charset=UTF-8");
header('Content-Length: '.$filesize);
header("location: ".$filename);
?>

如果我用小数据 (1mb) 触发脚本,它工作正常,但对于像 80mb 这样的大内容,它就不行了!

出了什么问题,如何解决?

不是最好的解决方案,但尽量把这一行放在代码的开头

ini_set('memory_limit','512M');

编辑

您似乎遇到了超时问题, 还添加此行:

set_time_limit(0);

重要提示: set_time_limit(0) 会将执行时间设置为无限制,不推荐这样做,但看看它是否解决了您的问题,如果它确实尝试优化超时