What potential server performance issues may arise when using readfile for files over 100 MB?
Potential server performance issues that may arise when using readfile for files over 100 MB include high memory usage and slow response times due to the entire file being read into memory before being sent to the client. To solve this issue, you can use a combination of readfile and output buffering to stream the file to the client in smaller chunks, reducing memory usage and improving performance.
$file = 'path/to/large/file';
if (file_exists($file)) {
$chunkSize = 1024 * 1024; // 1 MB chunk size
$handle = fopen($file, 'rb');
while (!feof($handle)) {
echo fread($handle, $chunkSize);
ob_flush();
flush();
}
fclose($handle);
} else {
echo 'File not found.';
}