我有以下代码试图让它下载服务器上的特定文件:
$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的"上传/"路径部分执行任何操作。
感谢您的任何帮助,我发现标题真的很令人困惑,为什么我不能只拥有"下载($file)"超出了我的范围。
编辑:我会为一个问题挣扎几天,最后决定在这里问,然后在自己修复后立即问。
我将文件的位置更改为与PHP脚本相同,但是我仍然想知道如何使其与它们分开一起使用。我还补充说:
readfile($file);
如果我用这两个更改进行更改,它不起作用。
还细线,新代码:
$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);
您应该在标题之后将文件的内容打印到 stdout:
$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));
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
echo $contents;
尝试更改此行:
header("Content-Disposition: attachment; filename='".basename($file)."';");