I have written a code that offers visitors to download a csv file which is of 1.1M in size. If one visits this script, download is interrupted around 30-40K (as shown below in wget output) while if he downloads it via a direct link like http://domain.com/events.csv it works just fine. I believe this has something to do with php configuration values on the server but i have played almost with all values[relevant and non-relevant] such as
- post_max_size [upto 90M]
- max_file_upload [upto 90M]
- max_execution_time [0 and upto 600]
- max_input_time [0 and upto 600]
- memory_limit [upto 1024M]
Following contains my code:
<?php $realpath="/home/user/public_html/events.csv"; $size = intval(sprintf("%u", filesize($realpath))); @ini_set('zlib.output_compression', 0); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download");; header("Content-Disposition: attachment;filename=events.xls"); header("Content-Transfer-Encoding: binary "); header("Pragma: public"); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: pre-check=0, post-check=0, max-age=0'); header ("Pragma: no-cache"); header("Expires: 0"); header("Content-Description: File Transfer"); header("Content-Type: text/csv"); header("Content-Length: ". $size); // also tried with having a flush(); here // also tried with file_get_contents(); //also tried with wrapping the file_get_contents() or readfile() call inside ob_start() and ob_flush()+ob_clean() readfile($realpath); exit; ?>
Here is the wget output
wget "http://domain.com/test.php" --2011-06-26 19:47:55-- http://domain.com/test.php Resolving domain.com... 126.96.36.199 Connecting to domain.com|188.8.131.52|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1139844 (1.1M) [text/csv] Saving to: `test.php' 3% [===> ] 36,373 --.-K/s in 11s 2011-06-26 19:48:11 (3.29 KB/s) - Connection closed at byte 36373. Retrying. --2011-06-26 19:48:12-- (try: 2) http://domain.com/test.php Connecting to domain.com|184.108.40.206|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1139844 (1.1M) [text/csv] Saving to: `test.php' 3% [===> ] 40,469 --.-K/s in 11s 2011-06-26 19:48:24 (3.66 KB/s) - Connection closed at byte 40469. Retrying.
If i remove the header() required to offer a download and just echo the contents, then Chrome shows that the test.php was around 1.09M plus some more request, adds upto 1.1M[even in this case wget of test.php shows same behaviour as above], while firefox firebug shows that request was between 140K-300K[still not displaying all contents.
Check http://www.php.net/manual/en/function.readfile.php and search for: large. Copy-paste the code 🙂