You are here: Re: Serving Files with PHP « PHP Programming Language « IT news, forums, messages
Re: Serving Files with PHP

Posted by Chuck Anderson on 11/17/06 23:14

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация