PHP-下载的图像已损坏
在服务器上正确上传图像,并且我上传文件夹中的文件与发送的文件相同。 它们也正确地显示在我的页面上,但下载后我无法使用它们 Windows image viewer告诉我“文件已损坏或太大,无法打开” 守则:PHP-下载的图像已损坏,php,apache,encoding,download,image-uploading,Php,Apache,Encoding,Download,Image Uploading,在服务器上正确上传图像,并且我上传文件夹中的文件与发送的文件相同。 它们也正确地显示在我的页面上,但下载后我无法使用它们 Windows image viewer告诉我“文件已损坏或太大,无法打开” 守则: $file= \FileQuery::create()->findPk($params["fileId"]); if ($file) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $filePath = $file->getFile
$file= \FileQuery::create()->findPk($params["fileId"]);
if ($file) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$filePath = $file->getFilepath();
header('Content-Type: '.finfo_file($finfo, $filePath));
finfo_close($finfo);
header('Content-Disposition: attachment; filename=' .basename($filePath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
flush();
readfile($filePath);
}
当我用Notepad++编辑图像时,我可以看到下载后整个文件都不同;它以“‰PNG”开头,但最终的文件以“X89PNG”开头,就像它以一种奇怪的方式编码一样
有线索吗
编辑:我以前发现过类似的问题,但答案对我来说并不适用。我的文件在上传前和下载后大小相同,只是内容编码不同
此外,该问题仅出现在图像文件中。
Pdf、zip、docx或文本文件都可以。
<?php
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".strlen($data));
exit($data);
$data = file_get_contents('your image url')
?>
可能重复的如果您提供答案,请解释您的代码并解释其不同的原因,这样您可以指出错误,我们就有机会了解一些东西。