Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    53
    Thanks
    8
    Thanked 4 Times in 4 Posts

    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!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •