我用PHP编写了一个下载脚本,直到昨天.今天我试着下载其中一个文件,发现它突然停止了工作:
PHP致命错误:第52行的E:\ home\tecnoponta\web\aluno\download.php中允许的内存大小为67108864字节(尝试分配119767041字节)
出于某种原因,PHP试图在内存中分配文件的大小,我不知道为什么.如果文件大小小于内存限制,我可以毫无问题地下载它,问题在于更大的文件.
我知道可以通过增加php.ini中的内存限制甚至在代码上使用ini_set来纠正它,但我想要一个更准确的方法来解决这个问题以及为什么它停止工作的答案.
这是我的代码:
$file = utf8_decode($_GET['d']);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$file = "uploads/$curso/$file";
ob_clean();
flush();
readfile($file);
echo "<script>window.history.back();</script>";
exit;
Run Code Online (Sandbox Code Playgroud)