【发布时间】:2015-07-04 14:33:46
【问题描述】:
我想使用 php 从我的服务器下载文件。我搜索了谷歌并找到了一个stackoverflow答案here。这个答案表明我必须为此编写这些代码。
$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");
那么我为什么要多写几行类似上面的代码呢?
【问题讨论】:
-
因为你现在做错了。文件名包含一个文件夹,您不知道该文件是什么类型。当然,它可能会起作用。但它也可能不会。请阅读这些标题上的文档。
-
你可以用这个。 file_put_contents($_SERVER['DOCUMENT_ROOT']."/directory", $file_url);第一个参数是文档根。第二个是你的文件。
-
好的,我会做,但如果它现在可以工作,为什么将来不能工作?
标签: php file download http-headers