Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-09-2012, 06:17 PM   PM User | #1
DarkLaika
New Coder

 
Join Date: Jan 2012
Posts: 53
Thanks: 8
Thanked 4 Times in 4 Posts
DarkLaika is an unknown quantity at this point
Post Directly display a file within a PHP file.

Hey there. Now I've been wondering how to do this for a while, I'm really quite confused. I need to have a PHP file that automatically displays a file. E.g.

http://www.example.com/file.php?file=image1

I need a script that would allow me to do this:

Code:
<img src="http://www.example.com/file.php?file=image1" />
and that would display an image. Same for CSS sheets, or any other file for that matter. Basically, the PHP file needs to be the image, if you get me.

Sorry if I've phrased this badly.

Thanks!
DarkLaika is offline   Reply With Quote
Old 02-09-2012, 07:07 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Served as an image. Simply push a header to adjust the content type to that of the data being output. In the case of a png for example it would be header('Content-type: image/png');. CSS doesn't need to be changed as its still an inherit of text/plain.

Once you access file.php, it uses the querystring to fetch the file itself, and then pulls the data from the file using a filereading technique (file_get_contents, fopen, readfile, fpassthru, fread, etc), and prints the binary as the returned result.
Fou-Lu is offline   Reply With Quote
Old 02-09-2012, 11:26 PM   PM User | #3
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
I'd like to add: Never, ever use something like file_get_contents($_REQUEST['something']) - you should at least use:

PHP Code:
echo(file_get_contents('source_folder/' basename($_REQUEST['something']))); 
Otherwise people can read hidden parts of your website by calling your script with ?something=index.php or similar.
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:43 PM.


Advertisement
Log in to turn off these ads.