php - 如何强制下载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/531292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php - How to force download of a file?
提问by Paolo Bergantino
I'm looking to add a "Download this File" function below every video on one of my sites. I need to force the user to download the file, instead of just linking to it, since that begins playing a file in the browser sometimes. The problem is, the video files are stored on a separate server.
我希望在我的一个网站上的每个视频下方添加“下载此文件”功能。我需要强制用户下载文件,而不仅仅是链接到它,因为有时会开始在浏览器中播放文件。问题是,视频文件存储在单独的服务器上。
Any way I can force the download in PHP?
有什么办法可以强制在PHP中下载?
回答by Paolo Bergantino
You could try something like this:
你可以尝试这样的事情:
$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.
我刚刚测试了它,它对我有用。
Please note that for readfileto be able to read a remote url, you need to have your fopen_wrappersenabled.
请注意,为了readfile能够读取远程 url,您需要fopen_wrappers启用。
回答by Elminson De Oleo Baez
Tested download.php file is
经过测试的download.php文件是
function _Download($f_location, $f_name){
$file = uniqid() . '.pdf';
file_put_contents($file,file_get_contents($f_location));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=' . basename($f_name));
readfile($file);
}
_Download($_GET['file'], "file.pdf");
and the link to download is
下载链接是
<a href="download.php?file=http://url/file.pdf"> Descargar </a>
回答by matpie
Try this:
尝试这个:
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
The key is the header(). You need to send the header along with the download and it will force the "Save File" dialog in the user's browser.
关键是header()。您需要将标题与下载一起发送,它会强制用户浏览器中的“保存文件”对话框。
回答by jeff lake
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
using this code. is it possible to save the file name to what you want. for example you have url: http://remoteserver.com/file.mp3instead of "file.mp3" can you use this script to download the file as "newname.mp3"
使用此代码。是否可以将文件名保存为您想要的名称。例如,您有网址:http://remoteserver.com/file.mp3 而不是“file.mp3”,您可以使用此脚本将文件下载为“newname.mp3”
回答by warodri
<?php
$file_name = 'video.flv';
$file_url = 'http://www.myserver.com/secretfilename.flv';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
echo file_get_contents($file_url);
die;
?>
回答by Nisharg Shah
I don't know this is the best way or not but I like that, short & simple.
我不知道这是不是最好的方法,但我喜欢这样,简短而简单。
If you want to downloadfile when you visit URL, you can do like below
如果你想在访问URL时下载文件,你可以像下面这样
<a href="resume.pdf" download></a>
<script>document.querySelector('a').click();</script>

