I am installing a website on my server, and it seems to have a problem with displaying images. When I enter the image URL, it first gives a 200 response, but then I see the following in the Chrome debugger:
Invalid
/ (failed)
/ net::ERR_FAILED
Request URL:chrome-extension://invalid/
Request Headers CAUTION: Provisional headers are shown.
Referer:http://tema-arc.co.il/index.php?item=file&action=download/786
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 OPR/22.0.1471.50 (Edition Campaign 47)
X-Source-Map-Request-From:inspector
I have traced the problem to the following function. In the beginning it got the CONTENT-LENGTH wrong, which I fixed, and the following is the updated code.
The file itself exists; when I try to output $sourceName
, it points to a real image file. The $size
is currently correct as well. In fact, it even does the dimensions right, as I can "zoom in" in the browser when I go to the image URL, but the image itself is not displayed. Needless to say that I have tried multiple browsers and multiple debuggers, but the Chrome debugger gave the only thing resembling an actual error.
function download($fileId)
{
$db = new Database();
@clearstatcache();
$file = $db->table('cms_files')->where(array('fileId' => $fileId))->select();
$sourceName = Config::get('uploadPath').$file[0]['source'];
if (!count($file) || !file_exists($sourceName))
{
$this->application->setNotFound();
exit;
}
$size = filesize($sourceName);
$type = $file[0]['type'];
$name = $file[0]['name'];
$lastModified = filemtime($sourceName);
header('Content-Description: File Transfer');
header('Content-Type: '.$type);
$ua = $_SERVER['HTTP_USER_AGENT'];
if (0 !== strpos($ua, 'Mozilla/4.0 (compatible; MSIE ')
|| false !== strpos($ua, 'Opera')) {
header('Content-Disposition: inline; filename="'.str_replace('"', '', basename($name)).'"');
}
else
{
header('Content-Disposition: inline; filename="'.mb_convert_encoding(str_replace('"', '', basename($name)),"ISO-8859-8", "auto").'"');
}
header('Content-Transfer-Encoding: binary');
header('Cache-Control: max-age=604800'); // Cache for one week.
header('Last-Modified: '.gmdate("D, d M Y H:i:s",$lastModified) . " GMT");
$ifLastModified = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
if($ifLastModified !== FALSE && $lastModified <= $ifLastModified)
{
header('HTTP/1.0 304 Not modified');
}
else
{
header('Content-Length: ' . $size);
ob_clean();
flush();
readfile($sourceName);
}
exit;
}