我正在通过我的网站强制下载图像。
强制下载在apache/windows开发机器上运行良好。
然而,当它在我的linux网络服务器上运行时,它会将垃圾字符推到屏幕上。
e.g. �����JFIF��H�H����6Exif��MM�*����
火狐-垃圾
铬-垃圾
Internet Explorer 7-在页面中显示图像
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: Image/jpeg");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
我的实时服务器上可能有什么差异会导致它崩溃?
最佳答案
hat tip(an d+1)tostillstanding,who pointed out usingfifo,但我想在这里提供一个示例来帮助您。这个例子需要安装fifo扩展,并且已经从我的其他一些代码中删除并稍作修改。
$filename = 'blarg.jpg';
$filepath = '/foo/bar/blarg.jpg';
$finfo = new finfo(FILEINFO_MIME);
$mime = $finfo->file($file);
// Provide a default type in case all else fails
$mime = ($mime) ? $mime : 'application/octet-stream';
header('Pragma: public');
header('Content-Transfer-Encoding: binary');
header('Content-type: ' . $mime);
header('Content-Length: ' . filesize($filepath));
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-transfer-encoding: 8bit');
header('Expires: 0');
header('Pragma: cache');
header('Cache-Control: private');