0

Image Link for testing

When I click the clink, Chrome, Safari and FF all automatically download a file rather the view it in the browser. How can I make it viewable in the browser? Using the link and a img tag works fine, it's just when used directly that it forces a download.

PHP

// Figure the mime type
$mimetypes = array(
    'png' => 'image/png',
    'jpg' => 'image/jepg',
    'gif' => 'image/gif',
    'css' => 'text/css',
    'js' => 'application/x-javascript'
);
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$mime = "application/octet-stream";
if(array_key_exists($ext, $mimetypes)) {
    $mime = $mimetypes[$ext];
}

header('Content-Type: ' . $mime);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
4

3 回答 3

0

编辑

问题是您错误地设置了内容类型。所以浏览器可能只是放弃尝试猜测它是什么,并要求您下载。

$ curl -D- http://wiki.justinzaun.com/buzz_aldrin/media/aldrin/250
HTTP/1.1 200 OK
Date: Thu, 20 Jun 2013 07:34:45 GMT
Server: Apache
Expires: 0
Cache-Control: must-revalidate
Pragma: public
Set-Cookie: PHPSESSID=49e5d03f1f0cf87119eef5bc6bb68d4d; path=/
Content-Length: 24909
Content-Type: image/jepg
于 2013-06-20T07:31:43.557 回答
0

我想你用jpg图像试过了。您的 $mimetypes 数组中有错字,这会导致 Content-Type 错误。当我修复它时,它对我来说很好。这是修复后的数组:

 $mimetypes = array(
    'png' => 'image/png',
    'jpg' => 'image/jpeg',
    'gif' => 'image/gif',
    'css' => 'text/css',
    'js' => 'application/x-javascript'
);
于 2013-06-20T07:32:24.123 回答
0

这一行有一个错误:

'jpg' => 'image/jepg', 

它应该是

'image/jpeg'.
于 2013-06-20T07:32:41.500 回答