【问题标题】:PHP file corruption when downloading .jpg files?下载.jpg文件时PHP文件损坏?
【发布时间】:2015-04-27 00:55:08
【问题描述】:

通过我网站上的 URL 下载 .jpg 文件时,文件已损坏。我没有测试很多文件,但 t.pdf 的下载很好。我在文件中看到以下错误:

b>警告:filesize() [function.filesize]: /home/content/91/4761691/html/pages/download.php中的sunny.jpg stat failed第 89 行

第 89 行是 filesize() 函数,如下所示...

$ID=stripslashes($_GET['recordid']);
$file = $_GET['file'];
// the absolute path to your directory storing your files.
$path = '../photos/' . $ID . '/';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
//header("Content-Disposition: attachment; filename=$file");
header('Content-Disposition: attachment;filename="'.$file.'"');
header("Content-Description: File Transfer");
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($file));   <========== line 89
readfile($path.$file);

关于为什么会失败以及如何解决的任何想法?

【问题讨论】:

  • $path.$file 的值是多少?

标签: php filesize


【解决方案1】:

你正在写readfile($path.$file);来输出文件,所以我假设你也应该写filesize($path.$file);来获取同一个文件的文件大小。

header('Content-Length: ' . filesize($path.$file));
readfile($path.$file);

【讨论】:

  • 好像明白了,谢谢!我的浏览器默认认为该文件是 .wav 文件但使用图像查看器应用程序打开是否有原因?我的文件关联是为图像查看器应用程序设置的...
  • 它可能与您正在传播的内容类型有关 (application/force-download)。尝试在此处提供适当的内容类型。
猜你喜欢
  • 2018-09-10
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 2014-06-05
  • 2014-09-01
  • 1970-01-01
相关资源
最近更新 更多