note 48683 modified in function.readfile by aidan

From: Date: Sat, 22 Jan 2005 08:29:52 +0000
Subject: note 48683 modified in function.readfile by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-83772@lists.php.net to get a copy of this message
When using readfile() with very large files, it's possible to run into problems due to the memory_limit setting; apparently readfile() pulls the whole file into memory at once. One solution is to make sure memory_limit is larger than the largest file you'll use with readfile(). A better solution is to write a chunking readfile. Here's a simple one that doesn't exactly conform to the API, but is close enough for most purposes: <?php function readfile_chunked ($filename) { $chunksize = 1*(1024*1024); // how many bytes per chunk $buffer = ''; $handle = fopen($filename, 'rb'); if ($handle === false) { return false; } while (!feof($handle)) { $buffer = fread($handle, $chunksize); print $buffer; } return fclose($handle); } ?> --was-- When using readfile() with very large files, it's possible to run into problems due to the memory_limit setting; apparently readfile() pulls the whole file into memory at once. One solution is to make sure memory_limit is larger than the largest file you'll use with readfile(). A better solution is to write a chunking readfile. Here's a simple one that doesn't exactly conform to the API, but is close enough for most purposes: function readfile_chunked ($filename) { $chunksize = 1*(1024*1024); // how many bytes per chunk $buffer = ''; $handle = fopen($filename, 'rb'); if ($handle === false) { return false; } while (!feof($handle)) { $buffer = fread($handle, $chunksize); print $buffer; } return fclose($handle); } http://php.net/manual/en/function.readfile.php

« previous php.notes (#83772) next »