Link to home
Start Free TrialLog in
Avatar of hmaloney
hmaloney

asked on

Displaying an image by reading the file and parsing byte code?

Hi, the way I wrote the question probably is a dead give away that I dont know what I'm talking about!

Background: security on the webserver is set up so that images in a particular folder cannot be browsed and displayed in a webbrowser unless they have a username and password on the folder, and this is the way it needs to stay.

What I need to do is this:  display the image file by some other means.  The webserver has access to the file by name.  I believe you can read the image file and display it somehow.  Can someone please help me with the code for this?

Thanks heaps,
Heather.
Avatar of gileze33
gileze33

You can find out the exact path of the server to the folder, I'll call it $path.

Then create a file such as images.php, then when you want say image.jpg, call images.php?img=image.jpg.

SOURCE OF images.php:

<?php

$img = $_GET['img'];
$path = "\path to images folder leaving in trailing slash\";

$instr = fopen($path.$img,"rb");
$bytes = fread($instr,filesize($path.$img));

header("Content-type: image/jpeg");
echo $bytes;

?>

Hope this helps :)

Gileze33.
Avatar of Robin Hickmott
or use imagejpeg()

http://uk2.php.net/imagejpeg
Yes but however, the imagejpeg() function needs GD to be installed.

Gileze33.
Avatar of hmaloney

ASKER

Thanks heaps gileze.

One thing I didn't mention is that I am displaying the image amongst lots of other text, so I don't think the headers line will work for me.

I've tried just printing out the bytes without that, however, and it wont work.  Can you please help?

Thanks again.
Yes thats understandable.

You need to treat the images.php file as though it were an image - so simply use the HTML <img> tag to load in the image to a separate HTML page.

E.g.

<html>
<body>
  Hi. Below is an image!
  <br>
  <img src="images.php?img=whatever.jpg" alt="wow - an image with a .php extension!">\
</body>
</html>

Sorry about the cheesy example :)

Gileze33.
Sorry, how does that work with the other code you gave me?
ASKER CERTIFIED SOLUTION
Avatar of gileze33
gileze33

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah!  You're a star.  I finally understood what you meant, and it worked like a gem.  Thank you!
No problem, glad to be of help.

Gileze33.