469,610 Members | 1,870 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

displaying a file not in the webroot

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?
Jan 11 '08 #1
5 3393
fi*********@yahoo.com wrote in news:8p5fo3dfmas4c1631shr2onc7jp991dph6@
4ax.com:
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?
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":

<?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";
readfile_chunked($thePathToTheFile);

//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;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;

}

?>
Jan 11 '08 #2
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?
Jan 11 '08 #3
fi*********@yahoo.com wrote in news:1pbfo3dbejpknghjlf35okvtppvk7g9j7b@
4ax.com:
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?
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.
Jan 11 '08 #4
fi*********@yahoo.com wrote:
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?
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));

readfile($file);

--
*****************************
Chuck Anderson Boulder, CO
http://www.CycleTourist.com
Nothing he's got he really needs
Twenty first century schizoid man.
***********************************

Jan 11 '08 #5
Thanks for the help. I am well on my way.
Jan 12 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by kafooey | last post: by
2 posts views Thread by Brian Richmond | last post: by
13 posts views Thread by raykyoto | last post: by
2 posts views Thread by John Carnahan | last post: by
9 posts views Thread by Joe Rattz | last post: by
15 posts views Thread by Jameson | last post: by
2 posts views Thread by Taras_96 | last post: by
1 post views Thread by strates | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.