I have the below function to output an image to the browser
function sendImage(string $file, int $browser_cache = 2592000) {
if (file_exists($file)) {
$mime = getimagesize($file)['mime'];
header("Content-Type: ". $mime);
header("Cache-Control: private, max-age=".$browser_cache);
header('Expires: '.gmdate('D, d M Y H:i:s', time()+$browser_cache).' GMT');
header('Content-Length: '.filesize($file));
readfile($file);
exit;
}
}
call the function
$file = $_SERVER['DOCUMENT_ROOT'] . '/images/image.png';
sendImage($file);
The content type output shows text/html and there is no response content. No errors either. The file definitely exists. I cant quite figure out the error in the function.