Today I was working on my website's function.php in an area that had nothing to do with serving images. I have a php script which gets images from outside of the webroot and serves them based on $_GET for the moment. One minute it was working and then the next it was not. I have no idea what happened. I chalked it up to MAC Sierra's BS server management but moving everything to a LINUX server made no difference.
<?php
include('includes/functions.php');
$path = $privatePath;
$uid = getUserProfileImage();
$img = $_GET['img'];
$file = $path."/img/users/".$uid."/".$img;
$fallback = "img/site/login.png";
//DETERMINE TYPE
$var = explode ('.', $img);
$ext = array_pop($var);
$allowed['gif'] = 'image/gif';
$allowed['png'] = 'image/png';
$allowed['jpg'] = 'image/jpeg';
$allowed['jpeg'] = 'image/jpeg';
if(file_exists($file) && $ext != '' && isset($allowed[strToLower($ext)])) {
$type = $allowed[strToLower($ext)];
} else {
$file = $fallback;
$type = 'image/png';
}
header('Content-Type: '.$type);
//header ("Content-Disposition: inline; filename=\"{$img}\");
header("Content-length: ".(string)(filesize($file)));
//echo file_get_contents($file);
readfile($file);
exit();
?>
This code was working just fine a from one min to the next. I have checked the permissions of the folder and verify that it is owned by www-data. Firefox reveals that the image has an error. Usually I had that error message when I was echoing something before the output buffer. But now it is for who knows what reason.
The RAW Data output in Firefox is as such when reading the image:
CgoKCv/Y/+EeQUV4aWYAAElJKgAIAAAADAAAAQMAAQAAAAACAAABAQMAAQAAAAACAAACAQMAAwAAAJ4AAAAGAQMAAQAAAAIAAAASAQMAAQAAAAEAAAAVAQMAAQAAAAMAAAAaAQUAAQAAAKQAAAAbAQUAAQAAAKwAAAAoAQMAAQAAAAIAAAAxAQIAHgA ...
And that goes on for a bit. I have resaved the image and about everything else that I can think of. I have even deleted all work done from prior when the script worked and no change. Anyone got any ideas? Or is there more error that I can log to see the problem and get a hint?