使用php下载文件,不能在大文件上工作? [英] using php to download files, not working on large files?
问题描述
我正在使用php下载文件,而不是文件本身在新窗口中打开。对于较小的文件来说似乎可以正常工作,但不适用于大型文件(我需要这个工作在非常大的文件)。以下是我必须下载文件的代码:
I'm using php to download files, rather than the file itself opening in a new window. It seems to work ok for smaller files, but does not work for large files (I need this to work on very large files). Here's the code I have to download the file:
function downloadFile($file) {
if (file_exists($file)) {
//download 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;
};
};
但是当我尝试下载一个大文件(例如265mb)时,浏览器告诉我,找到文件?这些文件一定在服务器上,脚本对于较小的文件可以正常工作。是否有任何方式可以下载类似于我已经拥有的大文件?
But when I try to download a large file (example 265mb) the browser tells me that it can't find the file? The files are definately on the server, and the script works fine for the smaller files. Is there any way of downloading large files similar to what I already have?
推荐答案
PHP对脚本可以运行多长时间有限制,以及它可以使用多少内存。脚本可能在完成之前超时,或者通过读入大文件来占用太多的内存。
PHP has limits on how long a script can run, and how much memory it can use. It's possible that the script is timing out before it has completed, or is using up too much memory by reading in the large file.
尝试调整 max_execution_time
和 memory_limit
变量在 php.ini
中。如果您无法访问 php.ini
,请尝试 set_time_limit
和/或 ini_set
功能。
Try tweaking the max_execution_time
and memory_limit
variables in php.ini
. If you don't have access to php.ini
, try the set_time_limit
and/or ini_set
functions.
这篇关于使用php下载文件,不能在大文件上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!