From: Php downloading empty file
SHARP OBJECTS EPISODE 1 TORRENT DOWNLOAD | 710 |
QIMERA DOWNLOAD FREE | 180 |
DOWNLOAD FINAL FANTASY 7 ISO PLAYSTATION | 157 |
DYNAMIC CRM FREE DOWNLOAD | 318 |
TF2 MVM TANK OBJ FILE DOWNLOAD | 567 |
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 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/www.cronistalascolonias.com.ar'; if (is_file($file)) { sendHeaders($file, 'image/jpeg', 'My www.cronistalascolonias.com.ar'); $string = @file_get_contents($file); if ($string !== FALSE) { echo $string; } exit; } ?>More advanced download
Using the 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 .
<?php $file = '/path/to/files/www.cronistalascolonias.com.ar'; if (is_file($file)) { sendHeaders($file, 'image/jpeg', 'My www.cronistalascolonias.com.ar'); 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/www.cronistalascolonias.com.ar'; if (is_file($file)) { sendHeaders($file, 'image/jpeg', 'My www.cronistalascolonias.com.ar'); $chunkSize = * ; $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.
Dimitar Ivanov is a senior LAMP developer, javascript engineer, web performance-obsessed. He is programming since and loves to build web applications. You can find him on Twitter, LinkedIn and GitHub.
Subscribe to our newsletter
Join our mailing list and stay tuned! Never miss out news about Zino UI, new releases, or even blog post.
6 Comments
Comments are closed
Источник: www.cronistalascolonias.com.ar
-
-
-