设置标题(content-type:image/<ANY IMG FORMAT>)

use*_*049 5 php substr http-headers

处理我显示的图像的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);
}
Run Code Online (Sandbox Code Playgroud)

小智 10

我只是用它,只是表明它是一个图像,但没有指定图像类型.

header("Content-type: image");
Run Code Online (Sandbox Code Playgroud)

无论文件类型如何,它似乎都工作得很好.我测试了IE,Firefox,Safari和Android浏览器.

  • 与`image/*`不同,`image`会在某些浏览器中内嵌图像,否则会提供带有内容类型`image/*`的图像作为文件下载. (3认同)

D55*_*555 7

你可以看看这个链接:: PHP,用Header()显示图像

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