如何强制使用PHP下载文件 [英] How to force file download with PHP

查看:147
本文介绍了如何强制使用PHP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要用户在使用PHP访问网页时下载文件。我认为这与 file_get_contents 有关,但不确定如何执行。

I want to require a file to be downloaded upon the user visiting a web page with PHP. I think it has something to do with file_get_contents, but am not sure how to execute it.

$url = "http://example.com/go.exe";

修改

下载文件后, code> header(location)它不会重定向到另一个页面。

Edit
After downloading a file with header(location) it is not redirecting to another page. It just stops.

推荐答案

阅读关于内置PHP函数的文档 readfile

Read the docs about built-in PHP function 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)

还要确保添加适当的内容根据您的文件应用程序/ zip,application / pdf等键入,但只有当您不想触发保存对话框时。

Also make sure to add proper content type based on your file application/zip, application/pdf etc. - but only if you do not want to trigger the save-as dialog.

这篇关于如何强制使用PHP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆