且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

php - 如何强制下载文件?

更新时间:2023-12-04 20:35:28

你可以尝试这样:

$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url);
exit;

我刚刚测试过,它适用于我。

I just tested it and it works for me.

请注意,对于 readfile 可以读取远程URL,您需要拥有 fopen_wrappers

Please note that for readfile to be able to read a remote url, you need to have your fopen_wrappers enabled.