【发布时间】:2013-11-03 21:28:47
【问题描述】:
我有以下代码试图下载服务器上的特定文件:
$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename="basename.$file);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
所发生的只是它导致浏览器下载一个文件,在我的例子中是“Order.txt”,但它是空白的,它只是在无处创建这个文件。它对 $file 的“upload/”路径部分没有任何作用。
感谢您的帮助,我发现标题真的很混乱,为什么我不能只拥有“下载($file)”是我无法理解的。
编辑:我会为一个问题苦苦挣扎好几天,最后决定在这里问,然后我自己解决后立即。
我将文件的位置更改为与 PHP 脚本相同,但我仍然想知道如何使其在它们分开的情况下工作。我还补充说:
readfile($file);
如果我更改了这 2 个更改中的一个,它将不起作用。
还有细线,新代码:
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
【问题讨论】:
-
php, file download的可能重复
-
你只需要1个
Content-Type头,发几个都没用。