363,925 Members | 2615 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Serving Files with PHP

David T. Ashley
P: n/a
David T. Ashley
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
Share this Question
Share on Google+
2 Replies


Chuck Anderson
P: n/a
Chuck Anderson
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

Pedro Graca
P: n/a
Pedro Graca
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

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP