【发布时间】:2013-12-07 09:34:45
【问题描述】:
我正在尝试实现 PHP 强制下载并使用下面的代码
$length = sprintf("%u", filesize($path));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($path);
exit;
文件以完整大小成功下载(我在记事本++中打开了文件,没有 php 错误或警告),但我无法打开文件。奇怪的是我可以毫无问题地下载和打开 pdf 文件。但是文件格式如 docx、png、jpg 是合适的
【问题讨论】:
-
这些反引号是干什么用的?
-
当我在这里提出问题时以某种方式添加它。它不存在于真实代码中
-
我在这里回答了类似的问题:stackoverflow.com/questions/23192683/…
-
它帮助了我 :) @Seti
标签: php force-download