Display various types files from server within php script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steelCity
Forum Newbie
Posts: 2
Joined: Mon May 04, 2009 6:33 pm

Display various types files from server within php script

Post by steelCity »

I was wondering how I would display various types of files within my php script. Example types of files would be .swf, .jpg, .html, .php, and so on. I need to display these types of files all on the same page, without necessarily knowing what the file extension is until I get the file path from my database. Thanks.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Display various types files from server within php scrip

Post by McInfo »

Some useful links:
Example usage:

Code: Select all

header('Content-Type: image/jpeg')
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 3:44 pm, edited 1 time in total.
steelCity
Forum Newbie
Posts: 2
Joined: Mon May 04, 2009 6:33 pm

Re: Display various types files from server within php script

Post by steelCity »

The issue is that I may need more options than jpeg for my files. Is there any better way, or generic way to do so.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Display various types files from server within php scrip

Post by McInfo »

That is what variables are for. Store the MIME type in the database.

Code: Select all

header('Content-Type: '.$mime_type_from_database);
You might want to use the Content-Disposition header too.

Code: Select all

header('Content-Disposition: attachment');
/* or */
header('Content-Disposition: attachment; filename=generic.jpg');
/* or */
header('Content-Disposition: attachment; filename="space name.jpg"');
/* Include quotes if you use spaces in the file name. Without quotes, the file would be downloaded as "space". */
/* or */
header('Content-Disposition: attachment; filename="'.$file_name_from_database.'"');
Often, file contents can be sent directly to the browser.

Code: Select all

echo file_get_contents($file_path_from_database);
/* or */
echo $file_contents_from_database;
Edit: This post was recovered from search engine cache.
Post Reply