设置标题(content-type:image / <any img =“”format =“”>)</any>

时间:2014-05-23 02:20:51

标签: php http-headers substr

处理我显示的图像的php文件只允许一种图像格式,.jpg,.png,.bmp等,但不是全部。 imageName存储数据库中存储的图像的文件名,包括其格式。这是我的代码,到目前为止还没有工作,我不确定是否允许这样做。你可以帮我解决一下吗?

$con = mysqli_connect("localhost","root","","tickets");
$ticket = 109;
$result = mysqli_query($con,"SELECT image, imageName FROM tix WHERE tktNum=$ticket");


while($row = mysqli_fetch_array($result))
{
    $image = $row['image'];
    $imageName = $row['imageName'];
    $format = substr( $imageName, -3 ); //gets the last 3 chars of the file name, ex: "photo1.png" gets the ".png" part
    header('content-type: image/' . $format);
}

2 个答案:

答案 0 :(得分:10)

我刚刚使用过这个,只是表明它是一个图像但没有指定图像类型。

header("Content-type: image");

无论文件类型如何,它似乎都运行得很好。我使用IE,Firefox,Safari和Android浏览器进行了测试。

答案 1 :(得分:7)

你可以看看这个链接::     PHP, display image with Header()

通过paullb检查答案,当您使用上述链接时,当然您还必须考虑用户评论中提到的更正