Page 1 of 1

Display various types files from server within php script

Posted: Mon May 04, 2009 6:41 pm
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.

Re: Display various types files from server within php scrip

Posted: Mon May 04, 2009 9:57 pm
by McInfo
Some useful links:
Example usage:

Code: Select all

header('Content-Type: image/jpeg')
Edit: This post was recovered from search engine cache.

Re: Display various types files from server within php script

Posted: Tue May 05, 2009 4:07 am
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.

Re: Display various types files from server within php scrip

Posted: Tue May 05, 2009 1:19 pm
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.