我想在我的网站上每个视频下面添加一个“下载此文件”功能。由于有时会在浏览器中播放文件,因此需要强制用户下载文件,而不是仅仅提供链接。问题是,视频文件存储在另一台服务器上。
有没有办法在PHP中强制下载?
我想在我的网站上每个视频下面添加一个“下载此文件”功能。由于有时会在浏览器中播放文件,因此需要强制用户下载文件,而不是仅仅提供链接。问题是,视频文件存储在另一台服务器上。
有没有办法在PHP中强制下载?
你可以尝试像这样做:
<?php
// Locate.
$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
// Configure.
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
// Actual download.
readfile($file_url);
// Finally, just to be sure that remaining script does not output anything.
exit;
我刚测试了一下,对我有效。
请注意,要使readfile
能够读取远程URL,您需要启用fopen_wrappers
。
readfile
读取文件,而不是远程读取,这样就可以避免双重带宽问题。 - Paolo Bergantino测试过的 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");
下载链接为
<a href="download.php?file=http://url/file.pdf"> Descargar </a>
试试这个:
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
关键在于header()
。您需要将头文件与下载一起发送,这将强制用户浏览器中出现“保存文件”对话框。
<?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;
?>
我不知道这是否是最佳方式,但我喜欢它,简短且简单。
如果你想要在访问 URL 时下载文件,可以像下面这样做:
<a href="resume.pdf" download></a>
<script>document.querySelector('a').click();</script>
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>
.htaccess
Options -Indexes
Options +FollowSymlinks
deny from all
download.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();
}
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
使用这段代码,你能否将文件名保存为你想要的名称呢?例如,你有一个URL:http://remoteserver.com/file.mp3,你能否使用这个脚本将文件下载为“newname.mp3”,而不是“file.mp3”?