phpdownloaddreamhostx-sendfile

PHP Download Script with mod-xsendfile Implementation


I'm currently using this PHP download script to a serve long list of large files (1GB+) from my website, but after complaints of corrupt downloads I researched and found a better looking alternative: mod_xsendfile. My hosting is with Dreamhost, and they've already enabled xsendfile on my domain. I tested it using this code from the authors website, and it works:

<?php 
header("X-Sendfile: /home/username/website.com/test.zip");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=test.zip");
exit;
?>

But I would like to have all of the downloads in one directory and link to the files like I do with the script from media division:

https://website.com/download.php?file=test.zip

I've searched through most of the questions tagged with x-sendfile and didn't find anything helpful. I can't write PHP code, but know enough to configure scripts to get them working. Does anyone know of a script that can do this or can help me out?

Thanks


Solution

  • That could be done like this:

    <?php 
    $path = realpath("/home/username/website.com/" . $_GET["file"]);
    if (strpos($path, "/home/username/website.com/") !== 0) {
        header("Status: 404 Not Found");
        die();
    }
    header("X-Sendfile: $path");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=".$_GET["file"]);
    

    This also makes sure the user can't get a file from any other directory then /home/username/website.com/, e.g. a database backup or something else you don't want to get downloaded.