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.
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.
Downloading large file with php
HTTP Status Code of 200 means everything executed okay.
Check that your Content-Length is correct, and try without the connection_status() function. It may be reporting improperly that the user disconnected. PHP has a connection_aborted() function which may be what you need.
Update: Because you asked about Mario's answer, what he means is that you should redirect your user to the actual file on the server and let your sever handle it instead of attempting to duplicate what the server can already do. In other words, you should use header("Location: " . $filePath)
, or a link to the filepath instead of using your script. The disadvantage is that you can't use the attachment style download, but you can open it in a new window, which will have a similar effect.
How to force download of big files without using too much memory?
There are some ideas over in this thread. I don't know if the readfile() method will save memory, but it sounds promising.
Downloading large private files using PHP
there are three reasons, I think , for this:
your script exceed the maximum duration for its execution
the speed of intenet, in the person who downloaded the file is too
slow. So your script will exceed the maximum duration for its
execution , as long as the user has not yet finished download the
filethe speed of your server is slow, and in this case no great thing
to do
a remedy for your problem
try to use the function: set_time_limit ()
**`set_time_limit (int $ seconds ) ;`**
eg set_limit_time tried ( 120 ) ;
will allow the user to download the file for less than 120 seconds
you must calculate how long you need to download the file, relative to the speed of the internet to your customers, and the speed of your server
add how long the script needs to execute
and this will be the time that you must give to the set_limit_time () function
I hope this has help
more info here : http://www.php.net/manual/en/function.set-time-limit.php
for more security use mod_xsendfile which is an apache module
mod_xsendfile is a small Apache2 module that processes X-SENDFILE headers registered by the original output handler.
If it encounters the presence of such header it will discard all output and send the file specified by that header instead using Apache internals including all optimizations like caching-headers and sendfile or mmap if configured.
It is useful for processing script-output of e.g. php, perl or any cgi.
Useful?
Yep, it is useful.
Some applications require checking for special privileges.
Other have to lookup values first (e.g.. from a DB) in order to correctly process a download request.
Or store values (download-counters come into mind).
etc.
Benefits
Uses apache internals
Optimal delivery through sendfile and mmap (if available).
Sets correct cache headers such as Etag and If-Modified-Since as if the file was statically served.
Processes cache headers such as If-None-Match or If-Modified-Since.
Support for ranges.
visit this link https://tn123.org/mod_xsendfile/
Related Topics
Blocking Comment Spam Without Using Captcha
How to Detect Duplicate Values in PHP Array
PHP MySQLi Prevent SQL Injection
PHP: Recreate and Display an Image from Binary Data
Converting an Integer to a String in PHP
Date Difference in PHP on Days
Error in PHP5 ..Unable to Load Dynamic Library
Posting Form Fields With Same Name Attribute
What Type of Hash Does Wordpress Use
Simple PHP Strpos Function Not Working, Why
How to Prevent Multiple Form Submission on Multiple Clicks in PHP
Jquery Ui Sortable, Then Write Order into a Database
Writing a New Line to File in PHP (Line Feed)
How to Add Custom Stock Status to Products in Woocommerce 4+