Solved

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

Posted on 2006-07-23
9
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

687 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