displaying a file not in the webroot

displaying a file not in the webroot

am 11.01.2008 17:27:39 von firewoodtim

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

am 11.01.2008 17:39:55 von Good Man

firewoodtim@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":


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;

}

?>

Re: displaying a file not in the webroot

am 11.01.2008 19:09:03 von firewoodtim

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:


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

am 11.01.2008 20:12:16 von Good Man

firewoodtim@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:
>
>
> 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




.... 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

am 11.01.2008 22:29:35 von Chuck Anderson

firewoodtim@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.
***********************************

Re: displaying a file not in the webroot

am 12.01.2008 01:03:19 von firewoodtim

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