PHP Fetch Files

Retrieve files from above the web root.

About This Website

SyntaxSeed is a collection of free and Open Source applications, themes, plugins and PHP scripts available for your next project.

This site is owned and operated by Avinus Web Services: www.avinus.com. Contact us for your next project or web hosting needs.
This script will grab files from above the web root to prevent direct linking. Users must go through this script to grab the file.
Source Code: + Expand <?php
/******************************************************************
* This script will stream files that are stored above the web root.
* Copyright © 2008 Sherri Wheeler. SyntaxSeed.com.
* Free for any use as long as copyright notice remains intact.
* Provided "as is" with no warranty.
*******************************************************************/

// CHECK FOR MEMBER LOGGED IN HERE, or include this into a file
// which already checks for member logged in.

// ****** Array of Private Files*******

$privateFilesPath[1] = "/home/username/privatedocs/file1.doc";
$privateFilesName[1] = "file1.doc";
$privateFilesMime[1] = "application/msword";

$privateFilesPath[2] ="/home/username/privatedocs/file2.txt";
$privateFilesName[2] = "file2.txt";
$privateFilesMime[2] = "text/plain";

// *************************

$index intval($_GET['id']);

if(
array_key_exists($index$privateFilesPath)){

    
$filepath $privateFilesPath[$index];

    
header('Content-Description: File Transfer');
    
header("Content-type: ".$privateFilesMime[$index]);
    
header("Content-Disposition: attachment; filename=".$privateFilesName[$index]);
    
header("Content-Length: " filesize($filepath));
    
header("Cache-Control: private"); // Required for IE6 opening PDFs.

    
if(ob_get_length()){
        
ob_clean();
    }
    
flush();
    
readfile($filepath);

}else{
   echo(
"File Not Found.");
}

exit(); 
// Remove this if including this script into another file.

?>