Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Displaying an image from PHP file

9:21 pm on Jul 4, 2009 (gmt 0)

New User

5+ Year Member

joined:July 4, 2009
posts: 21
votes: 0

Hello, I am having some issues with changing the URL of an image. What I want to do is load images through a PHP file.

for example, I have images in a folder, these images are also in my database(just the file names). I want to have the page with the image to use something like

<img src="get_file.php?file=$id"/>

//Im actually pulling the $id from database with:

<img src="get_file.php?file=<?php echo $seepic['hash'];?>"/>

where get_file.php has code like:

header('Content-type: image/jpeg');


$hash = $_GET['file'];

//select data from database
$pullinfo = mysql_query("SELECT `filename`,`hash`,`filetype`,`thumb`,`views` FROM `userfiles` WHERE hash = '$hash'") or die("SQL error: ".mysql_error());

$seepic = mysql_fetch_array($pullinfo);

$pic = 'image_folder/'.$seepic['filename'];

// load the file to send:

I have tried many variations of this code, using all sorts of methods found on this forum and others, but the result is always the same! The image does not display on the page.

If anyone can help me it would be great!


11:11 pm on July 4, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0

Make sure you have no redundant spaces/output that could render the image invalid (different browsers may handle this situation differently). ie. spaces before or after your PHP tags. Call exit; immediately after your readfile() to prevent trailing output.

Do you need to set the Content-Length header?

3:40 pm on July 5, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 27, 2003
votes: 0

A couple different options:

$image = imagecreatefromjpeg($path);
imagejpeg($image, NULL, 100);

^this reads the file then sends it to the browser.



^the fpassthru function is another way to send a file to the browser unmodified.

Also, if you use .htaccess at all for rewriting URLs, I'd just quickly recommend hiding the fact that you're using PHP to display the image. For example something like:

RewriteRule ^img/([a-z0-9_]+).jpg$ /get_file.php?file=$1

So your HTML code looks like <img src="/img/file_id123.jpg"> instead of <img src="get_file.php?file=file_id123"/>

5:03 pm on July 5, 2009 (gmt 0)

New User

5+ Year Member

joined:July 4, 2009
posts: 21
votes: 0

Thank you for your quick response, I have tried both methods before and again now, but with no success... this is driving me crazy.

When I open just the get_file.php with a get variable like


I get jarbled code if I set no header, and when I set the header the page contains only this :


im stumped as to what else I can try. My main goal for doing this is to hide the real location of the image/video file being displayed, then set a session variable for abit protection. I know this is not solid but its what I need.

[edited by: dreamcatcher at 6:40 pm (utc) on July 5, 2009]
[edit reason] use example.com. Thanks. [/edit]

2:38 pm on July 6, 2009 (gmt 0)

New User

5+ Year Member

joined:July 4, 2009
votes: 0

I got it SOLVED!

It turned out to be a blank space right after the ?> tag in my db_connect include.... WOW what a pain :)


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members