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
JSON_Encode() Non Utf-8 Strings
How to Get Email and Their Attachments from PHP
How to Generate Random Password with PHP
How to Get Open Graph Protocol of a Webpage by PHP
Stop Data Inserting into a Database Twice
How to Sort MySQL Fulltext Search Results by Relevancy
Determine If Business Is Open/Closed Based on Business Hours
Undefined Index While Uploading File
Pass Array Literal to Postgresql Function
How to Validate a Domain Name Using Regex & PHP
How to Pass an Array Using PHP & Ajax to JavaScript
PHP Files Are Downloaded by Browser Instead of Processed by Local Dev Server (Mamp)
Return PHP Object by Index Number (Not Name)
Best Way to Access Exchange Using PHP