PHP script to force download does not work

I had a simple little PHP code that allowed me to force users to download videos from my site rather than play it in a 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 resolution problem when I get 403 Forbidden "You do not have permission to access / download -stream.php on this server."

php file has the same rights as before (644). I'm not sure why he is doing this now.

0
source share
2 answers

I usually use something like this to force file uploads. Do not forget to change the MIME type if you change the type of the downloaded file.

        $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

- :

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

? >

0

All Articles