displaying a file not in the webroot

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I want to display an uploaded file (e.g. a pdf file) in a "_blank"
target window.  The file is stored outside of the webroot at
"/home/myhome/uploads/" and was uploaded by a trusted user with the
function, move_uploaded_file().

I've checked out readfile(), but there is scant info in the way of
examples on php.net.  Can anyone help with some code?

Re: displaying a file not in the webroot


Quoted text here. Click to load it

I use a custom function called "readfile_chunked" (on the 'readfile'  
manual page) because I've had bad luck with downloads stoppping at  
2MB... here are the nuts and bolts of my "streamFile.php":


header("Cache-control: private");  

//we stream the file, prompting a download
header('Content-Type: application/octet-stream');

// It will be called whatever we want (ie: "MyFile.pdf")
$theFileName = "MyFile.pdf";
header('Content-Disposition: attachment; filename="'.$theFileName.'"');

//this custom function is a good one for streaming files to browsers;  
//it does not suffer from a 2MB limit like "readfile();" sometimes does

$thePathToTheFile = "/home/myhome/uploads/superDuper.pdf";

//and here is that handy-dandy function
function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       if ($retbytes) {
           $cnt += strlen($buffer);
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile() does.
   return $status;



Re: displaying a file not in the webroot

Good Man,
Thanks for the info, but I'm still a bit confused.
What is the html that needs to be generated to enable launching the
file in a target="_blank"  window, and how do I get the data into it?

If the file had been under webroot, I could have just referenced the
file directly, as:
<a href="?images/MyFile.pdf" target="_blank">MyFile.pdf</a>

Now I have to put an "echo $buffer" in that new window somehow.  Can I
do it with the anchor, or do I need to write some javascript to call a
new window, or is there some other method that will be better?

Re: displaying a file not in the webroot


Quoted text here. Click to load it

Presumably you could just do

<a href="streamFile.php?fileID=xxxxx" target="_blank">MyFile.pdf</a>

... where streamFile.php first checks the fileID against a database,  
determines its name and location on the server, and does it's thang.

Re: displaying a file not in the webroot

firewoodtim@yahoo.com wrote:
Quoted text here. Click to load it

header("Cache-Control: cache, must-revalidate");
header("Pragma: public");

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($file));

// pick one

// to download
// header('Content-Disposition: attachment; filename=' . basename($file));

// to open in browser
// header('Content-Disposition: inline; filename=' . basename($file));


 Chuck Anderson • Boulder, CO
 Nothing he's got he really needs
 Twenty first century schizoid man.

Re: displaying a file not in the webroot

Thanks for the help.  I am well on my way.  

Site Timeline