What are potential pitfalls with using readfile and the PHP memory limit?
When using readfile in PHP to output large files, it can potentially exhaust the memory limit set in php.ini if the file size is too large. To avoid this issue, you can increase the memory_limit in php.ini or use other methods like fopen and fread to read and output the file in smaller chunks.
// Increase memory limit
ini_set('memory_limit', '256M');
// Read and output file in chunks
$filename = 'large_file.txt';
$handle = fopen($filename, 'rb');
while (!feof($handle)) {
echo fread($handle, 8192);
}
fclose($handle);