头文件下载创建新文件,而不是从服务器发送文件(PHP)
我有以下代码,试图下载服务器上的特定文件:头文件下载创建新文件,而不是从服务器发送文件(PHP),php,file,header,download,Php,File,Header,Download,我有以下代码,试图下载服务器上的特定文件: $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"); he
$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);
如果我用这两个改变中的一个改变,它就不起作用了
它的线条也很细,新代码:
$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);
尝试更改此行:
header("Content-Disposition: attachment; filename='".basename($file)."';");
您应该在标题后将文件内容打印到标准输出:
$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;
可能重复的您只需要1个
内容类型
标题,发送几个是无用的。文件名=
应该引用文件名=
小文件没有区别(对于大文件,我们可以逐部分读取以避免超出内存)顺便说一句,不要发送内容长度
标题,它们不需要,而且容易产生bug。