在php中强制下载

发布于 2025-01-07 05:29:21 字数 551 浏览 0 评论 0原文

我正在尝试创建一个强制下载页面,以防止浏览器打开应该下载的文件。问题是下载的文件有0字节,因此无法使用。我的代码有什么问题吗?

$file = "http://gh0stsec.zxq.net/background1.jpg";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
header("Content-Description: File Transfer");
@readfile($file);
exit();

I am trying to create a force-download page to prevent browsers from opening files that should be downloaded. The problem is that the downloaded file has 0 bytes and thus is unusable. What is wrong with my code?

$file = "http://gh0stsec.zxq.net/background1.jpg";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
header("Content-Description: File Transfer");
@readfile($file);
exit();

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

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

发布评论

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

评论(2

怪我太投入 2025-01-14 05:29:21

检查:

header("Content-Length: ".filesize($filename));

我认为应该是:

header("Content-Length: ".filesize($file));

check:

header("Content-Length: ".filesize($filename));

I think it should be:

header("Content-Length: ".filesize($file));
稀香 2025-01-14 05:29:21

我认为您会遇到 filesize 问题,因为它不是本地文件(如果是,请使用相对路径)。 Content-Type 标头对我来说总是有点奇怪,但在我读到的所有示例中,force-download 始终是一种后备。无论如何,我这样做了,它似乎有效:

<?php
$file = file_get_contents('http://gh0stsec.zxq.net/background1.jpg');

if ($file) 
{

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=background1.jpg');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . strlen($file));
    ob_clean();
    flush();

    echo $file;
} 
else   
{
    echo 'error';
}
?>

I think you're going to have an issue with filesize since it's not a local file (if it is, use a relative path). Content-Type headers are always a little weird for me, but in all of the examples I read, force-download was always a fallback. Anyways, I did this and it seemed to work:

<?php
$file = file_get_contents('http://gh0stsec.zxq.net/background1.jpg');

if ($file) 
{

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=background1.jpg');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . strlen($file));
    ob_clean();
    flush();

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