PHP 下载 .jpg 文件时文件损坏?

PHP file corruption when downloading .jpg files?

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

b>警告:filesize() [function.filesize]:/home/content/91/4761691/html/pages/download.php 中 sunny.jpg 的统计失败 在线 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);

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

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

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