图像文件下载问题

时间:2009-10-09 13:36:43

标签: php image download

我在从服务器下载任何图像文件时遇到问题。

我可以成功上传它们,并可以从上传和存储它们的位置打开它们。

当我使用我的功能下载时,图像文件被完全下载,文件大小也正确但是当我打开它时我得到一个错误没有图像预览!!!

$fileString=$fileDir.'/'.$fileName; // combine the path and file
    // translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
      $instance_name = preg_replace('/\./', '%2e', $instance_name, substr_count($instance_name, '.') - 1);
}
// make sure the file exists before sending headers

if(!$fdl=@fopen($fileString,'r'))
    {
       die("Cannot Open File!");
    } 
else 
    {
      header("Cache-Control: ");// leave blank to avoid IE errors
      header("Pragma: ");// leave blank to avoid IE errors
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
      header("Content-length:".(string)(filesize($fileString)));
      sleep(1);
      fpassthru($fdl);
    }

我使用IE作为浏览器。

我正在使用此代码段下载文件而不是在浏览器上显示。脚本执行,我会收到有关是否要打开/保存文件的提示。保存文件时,大小也正确但图像不显示。当我右键单击并查看文件摘要时,它表示摘要不可用。

提前致谢。请帮助。

3 个答案:

答案 0 :(得分:1)

您的代码中不清楚是否使用此PHP代码段在网页上提供图像,例如:

<img src="my-php-script.php" alt="blah blah blah" />

如果是这样,您的内容类型不正确。您需要使用图像MIME类型,例如image / gif,image / jpeg,image / png或image / tiff,以最合适的方式。

答案 1 :(得分:0)

这一行:header("Content-type: application/octet-stream");对我来说似乎很可疑。你可能想尝试给出实际的mime类型,看看是否有帮助

答案 2 :(得分:0)

问题在于MIME类型。请看这里:

http://www.daniweb.com/forums/thread122055.html

如果您想在浏览器中查看图像,请执行此操作。

如果您想将它们作为二进制文件下载,请保留您的mime类型。

另请将文件打开模式从“r”更改为“rb”

相关问题