我一直在努力找出问题所在,但每次我下载图像并尝试打开它时,都会发现文件已损坏。
$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);
也许可以尝试在readfile行之前添加以下两个命令。
ob_clean();
flush();
readfile($file);
这些行出现在readfile上的PHP文档的示例中。
试试这个:
$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;