强制下载的 PHP 脚本不起作用

发布于 2024-11-10 11:56:06 字数 358 浏览 4 评论 0原文

我有一个简单的小 php 代码,它允许我强制用户从我的网站下载视频,而不是在浏览器中播放它

<?php

$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);

?>

自从我将网站移动到新服务器后,似乎存在权限问题,我收到 403 Forbidden “您无权访问此服务器上的 /download-stream.php。”

php 文件设置为与之前相同的权限 (644)。我不知道为什么现在要这样做。

I had a simple little php code that allowed me to force users to download videos from my site rather than playing it in the browser

<?php

$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);

?>

Since I moved my site to a new server, there seems to be a permission problem where I am getting a 403 Forbidden "You don't have permission to access /download-stream.php on this server."

the php file is set to the same permissions as before (644). I'm not sure why it's doing this now.

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

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

发布评论

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

评论(2

渔村楼浪 2024-11-17 11:56:06

我通常使用类似的东西来强制下载。如果更改正在下载的文件类型,请记住更改 MIME 类型。

        $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/file.zip";
        if (file_exists($attachment_location)) {

            header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
            header("Cache-Control: public"); // needed for i.e.
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length:".filesize($attachment_location));
            header("Content-Disposition: attachment; filename=file.zip");
            readfile($attachment_location);
            die();        
        } else {
            die("Error: File not found.");
        } 

I generally use something like this to force downloads. Remember to change MIME type if you change the type of file being downloaded.

        $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/file.zip";
        if (file_exists($attachment_location)) {

            header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
            header("Cache-Control: public"); // needed for i.e.
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length:".filesize($attachment_location));
            header("Content-Disposition: attachment; filename=file.zip");
            readfile($attachment_location);
            die();        
        } else {
            die("Error: File not found.");
        } 
↙厌世 2024-11-17 11:56:06

尝试这样的事情:

<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$path\"\n");
$fp=fopen("$path", "r");
fpassthru($fp);

?>

Try something like this:

<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$path\"\n");
$fp=fopen("$path", "r");
fpassthru($fp);

?>

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