homepage Welcome to WebmasterWorld Guest from 54.198.46.115
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Displaying an image from PHP file
alexelisenko




msg:3946277
 9:21 pm on Jul 4, 2009 (gmt 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');

include'db_connect.inc';

$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:
readfile($pic);

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!

Thanks

 

penders




msg:3946303
 11:11 pm on Jul 4, 2009 (gmt 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?

FourDegreez




msg:3946513
 3:40 pm on Jul 5, 2009 (gmt 0)

A couple different options:

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

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

Or:

$fn=fopen($path,'r');
fpassthru($fn);

^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"/>

alexelisenko




msg:3946539
 5:03 pm on Jul 5, 2009 (gmt 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

get_file.php?file=7a6c5be0ef3933977e6264f3df831b3a

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

http://example.com/fancy/get_file.php?file=7a6c5be0ef3933977e6264f3df831b3a

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]

alexelisenko




msg:3946970
 2:38 pm on Jul 6, 2009 (gmt 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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved