【发布时间】:2011-01-23 23:12:56
【问题描述】:
我有以下强制下载文件的功能:
static public function download($file, $options=array()) {
$content = (isset($options['content'])) ? $options['content'] : '';
$contentType = (isset($options['contentType'])) ? $options['contentType'] : '';
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.File::filename($file));
header('Content-Type: '.$contentType);
header('Content-Transfer-Encoding: binary');
if ($content!='') {
echo $content;
} else {
readfile($file);
}
}
我发送一个 PDF 文件和 contentType = "application/pdf"。 问题是当我尝试打开下载的 PDF 文件时,它显示“打开此文档时出错。文件可能已损坏”。 很奇怪,因为我可以打开原始文件,它们看起来完全一样(文件名、大小等)
【问题讨论】:
-
运行前有输出吗?你能把
exit放在最后确保没有其他输出发送吗? -
谢谢亚历克斯,就是这样!我在附和一些东西,这就是问题所在......如果你愿意,你可以发表评论作为答案,这样我就可以将其投票为选定的人
标签: php pdf force-download