我试图显示要在<img />
标记中使用的图像,但此代码会显示损坏的图像。我知道该文件存在(请参阅file_exists行),我正在获取文件大小,并且正确设置了内容类型。谁能看到我失踪的东西?
$image_path = "../../../uploads/theimage.jpg";
$image_info = pathinfo($image_path);
$image_ext = $image_info["extension"];
switch( $image_ext ) {
case "gif":
$content_type="image/gif";
break;
case "png":
$content_type="image/png";
break;
case "jpeg":
case "jpg":
$content_type="image/jpeg";
break;
default:
die("invalid image type");
break;
}
if (file_exists($image_path)) {
header('Content-Type:'.$content_type);
header('Content-Length: ' . filesize($image_path));
echo file_get_contents($image_path);
} else {
echo 'no file?';
}