Categories
Download Large Files with PHP
Often a simple task as file downloading may lead to an out of memory. To accomplish successfully file download exists few approaches. Let's see the options:
Send headers
When starting a file download you need to send the proper headers to the browser.
<?php function sendHeaders($file, $type, $name=NULL) { if (empty($name)) { $name = basename($file); } header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private', false); header('Content-Transfer-Encoding: binary'); header('Content-Disposition: attachment; filename="'.$name.'";'); header('Content-Type: ' . $type); header('Content-Length: ' . filesize($file)); } ?>
Simple download
Using the file_get_contents()
may be it's not the best choise,
but should works perfectly for small files. The main disadvantage is that when
you store the file contents into a variable, the memory for it is reserved.
<?php $file = '/path/to/files/photo.jpg'; if (is_file($file)) { sendHeaders($file, 'image/jpeg', 'My picture.jpg'); $string = @file_get_contents($file); if ($string !== FALSE) { echo $string; } exit; } ?>
More advanced download
Using the readfile()
will not present any memory issues, even
when sending large files, on its own. If you encounter an out of memory error
ensure that output buffering is off with ob_get_level()
.
<?php $file = '/path/to/files/photo.jpg'; if (is_file($file)) { sendHeaders($file, 'image/jpeg', 'My picture.jpg'); ob_clean(); flush(); @readfile($file); exit; } ?>
Chunked download
This is the old fashioned, but still the most right way to download large files with PHP.
<?php $file = '/path/to/files/photo.jpg'; if (is_file($file)) { sendHeaders($file, 'image/jpeg', 'My picture.jpg'); $chunkSize = 1024 * 1024; $handle = fopen($file, 'rb'); while (!feof($handle)) { $buffer = fread($handle, $chunkSize); echo $buffer; ob_flush(); flush(); } fclose($handle); exit; } ?>
Basically all the presented three methods can be used to force downloading a file, but when it comes to large files the chunked download is the most right way.
If you have questions about downloading large files in small chunks in PHP, leave a comment below. And do not be shy to share this article. Thanks for reading.
Subscribe to our newsletter
Join our mailing list and stay tuned! Never miss out news about Zino UI, new releases, or even blog post.
0 Comments
Comments are closed