How does the choice between a header-redirect and readfile in PHP impact server performance when serving images from the filesystem cache?
When serving images from the filesystem cache in PHP, using readfile to directly output the image content is more efficient than using header-redirect to redirect to the image file. This is because readfile directly streams the image content to the client without unnecessary redirection steps, reducing server load and improving performance.
// Using readfile to serve images from the filesystem cache
$imagePath = '/path/to/image.jpg';
if (file_exists($imagePath)) {
header('Content-Type: image/jpeg');
readfile($imagePath);
exit;
} else {
header("HTTP/1.0 404 Not Found");
echo "Image not found";
}