Solved

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

Posted on 2006-07-23
9
266 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

22 Experts available now in Live!

Get 1:1 Help Now