473,399 Members | 3,656 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,399 software developers and data experts.

Serving Files with PHP

I've used Apache with the automatic directory indexes ... works great ...
just click on the file name and IE magically figures out how to display it
.... if it is a .PDF then Adobe Acrobat runs, etc.

However, I'd like to "serve" files in a more restrictive context where:

a)The user has to have a valid session identifier in order to get a file.

b)The files I'd be serving aren't in directories that Apache can get to
directly, i.e. not in a "Document Root" or aliased directory (although it
has the right permissions to get the files).

Is it possible to do this with PHP?

Can PHP just spit out the right MIME information and then somehow open and
encode the file and the client will treat it properly?

Are there any PHP functions that help with the encoding?

Are there any good web references?

Thanks

Nov 17 '06 #1
2 8891
David T. Ashley wrote:
I've used Apache with the automatic directory indexes ... works great ...
just click on the file name and IE magically figures out how to display it
... if it is a .PDF then Adobe Acrobat runs, etc.

However, I'd like to "serve" files in a more restrictive context where:

a)The user has to have a valid session identifier in order to get a file.

b)The files I'd be serving aren't in directories that Apache can get to
directly, i.e. not in a "Document Root" or aliased directory (although it
has the right permissions to get the files).

Is it possible to do this with PHP?
Yes.
Can PHP just spit out the right MIME information
Yes.
and then somehow open and encode the file and the client will treat it properly?
If the mime type is right, the client should be able to treat it properly.
Are there any PHP functions that help with the encoding?
No encoding is needed.

You can add headers with header and serve it with readfile or include.
Are there any good web references?
Php.net usually has lots of good notes added by users.

Here's how I serve a pdf file that exists outside of the site home
directory.

$file = 'path to the pdf file';

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

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

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

readfile($file); /* or use include($file); */

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Nov 17 '06 #2
David T. Ashley wrote:
a)The user has to have a valid session identifier in order to get a file.
Why?
Or, to put it differently, what do you mean "valid session identifier"?
And what good does it do to you when the "user has one"?
b)The files I'd be serving aren't in directories that Apache can get to
directly, i.e. not in a "Document Root" or aliased directory (although it
has the right permissions to get the files).
readfile()
http://php.net/readfile
Is it possible to do this with PHP?
Yes.
Can PHP just spit out the right MIME information and then somehow open and
encode the file and the client will treat it properly?
See mime_content_type()
http://php.net/mime_content_type
Are there any PHP functions that help with the encoding?
What do you mean?
Maybe some of the function in http://php.net/recode or
http://php.net/unicode would help, but I doubt it.
Are there any good web references?
Yes. The PHP manual :)
http://php.net/manual
--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Nov 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gooseman | last post by:
Hi, I have a page where form data is passed to a php page (just script) to process the input. This page calls a function to output a file: function sendFileViaHeader($path, $name) { $fp =...
0
by: Benjamin Cabé | last post by:
Hi, I have to serve TIFF images (scanned documents) stored on an unique server, clients are likely to edit these images and send them back to the server.. Ideally, the client would ask something...
21
by: Nicholas Sherlock | last post by:
Hey all, People on my website register to be allowed access to certain downloads. I store these files above the document root so that they can't be accessed by Apache (Only from PHP). I wrote a...
2
by: David | last post by:
I am installing apps on an asp.net 1.1 machine to which I have no access to the IIS configuration. I need to lock out the viewing of files types in a directory. As per info on the net, I added...
3
by: Prash | last post by:
IIS serving javascript files, gives me error 4/21/2006 6:48 AM Guys/Gals, I am using IIS as a server to serve the javascript files. But it is giving me the error as "Object doesn't support this...
1
by: Dennis Benzinger | last post by:
Hi! I want to serve files which are stored as attachments on an IMAP server. Is there a module which does this or do I have to write my own? Dennis
2
by: martin.fowler | last post by:
Hi everyone, I dont know if this is the correct place for this question or not, but here it goes. I have multiple projects that I use the same code for. I wanted to know if it was possible to load...
3
by: Sander Tekelenburg | last post by:
Situation: I store news articles as individual PHP files. Each file contains HTML and now and then some embedded PHP snippets. Serving those news articles on the Web works fine, through...
10
by: Robert Huff | last post by:
Can someone offer suggestions why, on the same server (Apache 2.2.8), this works <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en-US"> <head> <link rel=stylesheet...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.