如何使用php下载文件?

如何使用php下载文件?,php,file,download,http-headers,Php,File,Download,Http Headers,我想用php从我的服务器下载文件。我搜索了谷歌,找到了一个答案。这个答案表明我必须为此编写这些代码 $file_url = 'http://www.myremoteserver.com/file.exe'; header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=

我想用php从我的服务器下载文件。我搜索了谷歌,找到了一个答案。这个答案表明我必须为此编写这些代码

$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) .     "\""); 
readfile($file_url); 
但我能做到这一点,只需这两行:

header("content-disposition:attachment; filename=uploads1/EFL1.5_Setup.exe");
readfile("uploads1/EFL1.5_Setup.exe");
那么,我为什么还要像上面的代码那样多写几行呢

header('Content-Type: application/octet-stream');
内容类型应该是已知的任何类型,如果您知道的话。在RFC 2046中,应用程序/八位字节流被定义为“任意二进制数据”,这里有一个明确的重叠,它适用于唯一目的是保存到磁盘的实体,从那时起,它就不属于任何“webby”。或者从另一个方向看它;使用application/octet流唯一安全的方法是将其保存到文件中,并希望其他人知道它的用途

header("Content-Transfer-Encoding: Binary"); 
内容传输编码指定用于在HTTP协议内传输数据的编码,如原始二进制或base64。(二进制比base64更紧凑。base64有33%的开销)

参考:


以下是下载文件的代码,其中包含下载百分比的信息:



因为你现在做错了。文件名包含一个文件夹,而您不知道该文件的类型。当然,它可能有用。但也可能不是。请务必阅读这些标题上的文档。您可以使用此标题。文件内容($服务器['DOCUMENT\u ROOT']。“/directory”,$file\u url);第一个参数是文档根。第二个是你的文件。好的,我会的,但是如果它现在起作用,为什么将来可能不起作用呢?为什么这个被否决了?这个答案怎么了?