我一直想弄清楚什么是错的,但每次下载图片并尝试打开图片时,都会说文件已损坏。
$ h是从数据库中提取的路径,$ h成功显示页面上的图像,但我不明白为什么它不会下载。有任何想法吗 ??
header("Pragma: public"); // required
header("Cache-Control: private",false); // required for certain browsers
header('Content-Length: '. filesize("../".$h));
header('Content-Type: application/octet-stream');
header('Content-Disposition: inline; filename="'.md5($h).$ext.'"');
header('Content-Transfer-Encoding:binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile("../".$h);
答案 0 :(得分:8)
也许尝试在readfile行之前添加以下两个命令。
ob_clean();
flush();
readfile($file);
这些行是在readfile上的PHP文档的示例中。
答案 1 :(得分:2)
试试这个:
$localPath = realpath("../$h");
if (!file_exists($localPath)) {
exit("Cannot find file located at '$localPath'");
}
header('Pragma: public'); // required
header('Content-Length: '.filesize($localPath));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.md5($localPath).'.'.$ext.'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false); // required for certain browsers
readfile($localPath);
exit;