PHP Script to Force Download Not Working

*爱你&永不变心* 提交于 2020-01-06 08:44:10

问题


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.


回答1:


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.");
        } 



回答2:


Try something like this:

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

?>



来源:https://stackoverflow.com/questions/6196154/php-script-to-force-download-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!