473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3578
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kafooey | last post by:
Hi all, I've been digging around on the newsgroups and the web for a possible answer for this problem, but have so far come up with nothing so thought I would ask here... I have the following...
2
by: Brian Richmond | last post by:
I'm writing a script to upload images along with articles to a directory on the server. I'm developing it offline on my WinXP and Apache 1.3.x laptop and it's working great, but when I move the...
13
by: raykyoto | last post by:
Hi all, I'm sure this is a popular question that comes up every few months here. Indeed, I've looked at some of the past postings, but I would like to ask things differently. Basically, I'm...
2
by: John Carnahan | last post by:
Using .net in aspx page with following code, and get a write error on the website, but everything works fine on the development machine. ----- code ---- Dim counter As String = CType(logcount,...
9
by: Joe Rattz | last post by:
I can't seem to get to trace.axd. I have turned tracing on in web.config. Here is how I currently have i configured: <trace enabled="true" requestLimit="10" pageOutput="false"...
15
by: Jameson | last post by:
Happy New Year, Everyone! I am trying to figure out how to display a bunch of images (mainly JPEGs, but possibly a few GIFs and PNGs as well) that are stored in a local directory on the system....
2
by: Taras_96 | last post by:
Hi all, I've been using debug_backtrace() for a while to provide stack traces for a custom logging function. Today was the first time I've got the following error: "Notice: Undefined index:...
1
by: strates | last post by:
Hello, I have been developing an ajax/php app. In development I have kept everything in the webroot for ease, but as the project matures, I would like to move all the application code outside the...
2
by: Francesco | last post by:
Hi there! I'm trying to organize my sources into a webroot tree like this, webroot index.htm - only contains index.php into a frame index.php - require_once('inc/layout.php') ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.