发布日期:2018-03-26
如何用PHP强制下载文件+ 查看更多
如何用PHP强制下载文件
+ 查看更多
发布日期:2018-03-10 16:50
分类:PHP
浏览次数:89
我希望用户访问PHP网页时下载文件。我认为它与file_get_contents有关,但不知道如何执行它
$url = "http://example.com/go.exe";
编辑
下载带有header(location)的文件后,它不会重定向到另一个页面,只是停止
回答:
阅读关于内置PHP函数readfile的文档
$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); // do the double-download-dance (dirty but worky)还要确保基于您的文件添加正确的内容类型如application/zip, application/pdf 等等 - 但这只有当你不想触发保存对话框。