php-如何强制下载文件?
我正在寻找添加一个“下载此文件”功能下面的每一个视频在我的网站之一。我需要强迫用户下载文件,而不是仅仅链接到它,因为这有时会开始在浏览器中播放文件。问题是,视频文件存储在单独的服务器上php-如何强制下载文件?,php,download,remote-server,Php,Download,Remote Server,我正在寻找添加一个“下载此文件”功能下面的每一个视频在我的网站之一。我需要强迫用户下载文件,而不是仅仅链接到它,因为这有时会开始在浏览器中播放文件。问题是,视频文件存储在单独的服务器上 我可以用PHP强制下载吗 您可以尝试以下方法: $file_name = 'file.avi'; $file_url = 'http://www.myremoteserver.com/' . $file_name; header('Content-Type: application/octet-stream');
我可以用PHP强制下载吗 您可以尝试以下方法:
$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;
我刚刚测试过,它对我有效
请注意,要使readfile
能够读取远程url,您需要启用。请尝试以下操作:
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
已测试下载.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");
下载的链接是
<a href="download.php?file=http://url/file.pdf"> Descargar </a>
我不知道这是不是最好的方式,但我喜欢这样,简短而简单
如果您想在访问URL时下载文件,您可以执行以下操作
document.querySelector('a')。单击();
index.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>download</title>
</head>
<body>
<a href="download.php?file=ID_do_arquivo"> download </a>
</body>
</html>
下载.php
$file = "pdf/teste.pdf";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
只需记住:str_replace(“”、“\\”、$file\u name)然后再将其放入头部。该漏洞是所有视频都将通过他的服务器运行。如果由于带宽原因,视频位于单独的服务器上,则此解决方案将不可接受。然而,从问题的声音来看,我得到的印象是,他没有访问这个远程服务器的权限,否则这将是一个微不足道的问题。如果是这样的话,据我所知,这将是唯一的解决方案。我认为使用readfile将消耗两个站点的带宽。有人想这样做的主要原因是为了节省一些钱bandwidth@Ben如果您有权访问包含该文件的服务器,您只需从该服务器readfile
,而不必远程访问,消除双带宽问题。可能重复的请提供解决方案的更详细说明。虽然您的代码看起来很棒,并且很可能回答了问题,但请添加对其工作原理的解释,以便人们理解其工作原理。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>download</title>
</head>
<body>
<a href="download.php?file=ID_do_arquivo"> download </a>
</body>
</html>
Options -Indexes
Options +FollowSymlinks
deny from all
$file = "pdf/teste.pdf";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}