Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-07-23
9
Medium Priority
?
292 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
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 1000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 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