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

时间:2011-06-01 04:07:13

标签: php apache permissions http-status-code-403

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

<?php

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

?>

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

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

2 个答案:

答案 0 :(得分:1)

我通常使用这样的东西来强制下载。如果更改了正在下载的文件类型,请记住更改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.");
        } 

答案 1 :(得分:0)

尝试这样的事情:

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

&GT;