Solved

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

Posted on 2006-07-23
9
269 Views
Last Modified: 2008-02-07
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.
0
Comment
Question by:hmaloney
  • 5
  • 3
9 Comments
 
LVL 3

Expert Comment

by:gileze33
ID: 17163017
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.
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 17163406
or use imagejpeg()

http://uk2.php.net/imagejpeg
0
 
LVL 3

Expert Comment

by:gileze33
ID: 17163416
Yes but however, the imagejpeg() function needs GD to be installed.

Gileze33.
0
 

Author Comment

by:hmaloney
ID: 17164252
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:gileze33
ID: 17166643
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.
0
 

Author Comment

by:hmaloney
ID: 17166831
Sorry, how does that work with the other code you gave me?
0
 
LVL 3

Accepted Solution

by:
gileze33 earned 250 total points
ID: 17166875
Ok.

Make images.php with the source at the top, replacing the absolute path with one to your images folder.

Next, create file.html, with the source I put above (making sure that you change the source for the image to be one that actually exists).

Hope this helps :)

Gileze33.
0
 

Author Comment

by:hmaloney
ID: 17167016
Ah!  You're a star.  I finally understood what you meant, and it worked like a gem.  Thank you!
0
 
LVL 3

Expert Comment

by:gileze33
ID: 17167263
No problem, glad to be of help.

Gileze33.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now