php中图像下载的未知文件格式

时间:2012-12-14 05:55:09

标签: php image download

我有这个代码在PHP中的图像下载...工作正常,图像下载,但问题是,它不会在下载的地方打开,并给出错误“无法读取文件标题.. 。未知的文件格式!“

<?php
$path = $row['img_url'].".jpg";
echo $path;
$filename = $path;
$ctype="application/.jpg";
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

4 个答案:

答案 0 :(得分:3)

更改内容类型

$ctype="application/.jpg"; //It's a invalid content type

$ctype="image/jpeg";

答案 1 :(得分:2)

尝试使用image/jpeg MIME类型而不是application/.jpg

答案 2 :(得分:2)

你不应该在标题之前回显任何东西并尝试使用标题('Content-Type:image / jpeg');

答案 3 :(得分:1)

您的内容类型有误。尝试使用$ctype="image/jpeg";

相关问题