Using PHP to Download Files, Not Working on Large Files

using php to download files, not working on large files?

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.

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.

Big files download through php function readfile not working

Here is the complete solution thanks to the answer of witzawitz:

I needed to use ob_end_flush() and fread();

<?php 
$sysfile = '/var/www/html/myfile';
if(file_exists($sysfile)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="mytitle"');
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($sysfile));
ob_clean();
ob_end_flush();
$handle = fopen($sysfile, "rb");
while (!feof($handle)) {
echo fread($handle, 1000);
}
}
?>

How to download large files through PHP script

If you use fopen and fread instead of readfile, that should solve your problem.

There's a solution in the PHP's readfile documentation showing how to use fread to do what you want.

php download fails on large files

The only thing I do different in my downloader is Content-Transfer-Encoding: chunked. Also take the flush() out of the loop and make sure you do ob_clean(); flush(); before and ob_end_flush(); after sending the data.



Related Topics



Leave a reply



Submit