473,406 Members | 2,345 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,406 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 3579
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.