I'm trying to display an image using a PHP script. Basically so the php script is passed on the full path to the image, and it then displays that image in the browser. I've checked to make sure the image exists, it is being read correctly, etc, however in the browser i just see the broken image box (e.g the small red cross in IE) if I go there.
我正在尝试使用PHP脚本显示图像。基本上,php脚本在图像的完整路径上传递,然后在浏览器中显示该图像。我已经检查过以确保图像存在,正确读取等,但是在浏览器中我只是看到了破碎的图像框(例如IE中的小红叉),如果我去那里的话。
My script sends out these headers:
我的脚本发出这些标题:
<?php
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', filemtime($file)));
header('Content-Type: '.$mime);
header('Content-Length: '.filesize($file)."\n\n");
header('Etag: '.md5($file));
echo $file;
die;
$file
contains something like '/var/www/htdocs/images/file.jpg'
which works. the $mime
is 'image/jpeg'
.
$ file包含类似'/var/www/htdocs/images/file.jpg'的内容。 $ mime是'image / jpeg'。
I have also tried echoing file_get_contents($file)
but it didn't work either.
我也试过echoing file_get_contents($ file),但它也没有用。
What is the problem, any thoughts?
有什么问题,有什么想法吗?
3 个解决方案
#1
<?php
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension )
{
case "gif":
$ctype="image/gif";
break;
case "png":
$ctype="image/png";
break;
case "jpeg":
case "jpg":
$ctype="image/jpeg";
break;
default:
}
ob_clean(); // add this before header
header('Content-type: ' . $ctype);
readFile($file);
?>
Some time extra space may be produced in any other file.So those extra spaces can be removed by calling ob_clean() before header() is called.
有时可能会在任何其他文件中产生额外的空间。因此,在调用header()之前调用ob_clean()可以删除这些额外的空格。
#2
Striving for simplicity...
力求简单......
<?php
header('Content-type:' . mime_content_type($file));
readfile($file);
should function as expected.
应该按预期运作。
#3
I found the answer, i had some extra whitespace after the ?>
tag which was causing the header to not work. grrrrrrr
我找到了答案,我在?>标签后面有一些额外的空格,这导致标头不起作用。 grrrrrrr
#1
<?php
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension )
{
case "gif":
$ctype="image/gif";
break;
case "png":
$ctype="image/png";
break;
case "jpeg":
case "jpg":
$ctype="image/jpeg";
break;
default:
}
ob_clean(); // add this before header
header('Content-type: ' . $ctype);
readFile($file);
?>
Some time extra space may be produced in any other file.So those extra spaces can be removed by calling ob_clean() before header() is called.
有时可能会在任何其他文件中产生额外的空间。因此,在调用header()之前调用ob_clean()可以删除这些额外的空格。
#2
Striving for simplicity...
力求简单......
<?php
header('Content-type:' . mime_content_type($file));
readfile($file);
should function as expected.
应该按预期运作。
#3
I found the answer, i had some extra whitespace after the ?>
tag which was causing the header to not work. grrrrrrr
我找到了答案,我在?>标签后面有一些额外的空格,这导致标头不起作用。 grrrrrrr