php - 如何强制下载文件?

发布于 2024-07-13 06:23:12 字数 128 浏览 5 评论 0原文

我希望在我的网站上的每个视频下方添加“下载此文件”功能。 我需要强制用户下载文件,而不仅仅是链接到它,因为有时会开始在浏览器中播放文件。 问题是,视频文件存储在单独的服务器上。

有什么办法可以强制 PHP 下载吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

栖竹 2024-07-20 06:23:12

你可以尝试这样的事情:

<?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 已启用。

You could try something like this:

<?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;

I just tested it and it works for me.

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

惜醉颜 2024-07-20 06:23:12

测试过的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>

Tested download.php file is

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>
放血 2024-07-20 06:23:12

试试这个:

<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);

关键是 header()。 您需要随下载一起发送标头,它将强制用户浏览器中显示“保存文件”对话框。

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.

掀纱窥君容 2024-07-20 06:23:12
<?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;

?>
<?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;

?>
山川志 2024-07-20 06:23:12

我不知道这是否是最好的方法,但我喜欢这样,简短且简单。 简单的。

如果您想在访问URL下载文件,您可以执行以下操作

<a href="resume.pdf" download></a>
<script>document.querySelector('a').click();</script>

I don't know this is the best way or not but I like that, short & simple.

If you want to download file when you visit URL, you can do like below

<a href="resume.pdf" download></a>
<script>document.querySelector('a').click();</script>
最偏执的依靠 2024-07-20 06:23:12

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

下载.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();
}

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();
}
分開簡單 2024-07-20 06:23:12
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);

使用此代码。 是否可以将文件名保存为您想要的名称。 例如,您有 url: http://remoteserver.com/file.mp3 而不是“file.mp3” mp3” 您可以使用此脚本将文件下载为“newname.mp3”

<?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.mp3 instead of "file.mp3" can you use this script to download the file as "newname.mp3"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文