How to disp pics outside www-root folder?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
nickvh
Forum Newbie
Posts: 11
Joined: Sat May 31, 2003 3:37 pm
Location: VA, USA
Contact:

Post by nickvh »

stuart wrote:
Galt wrote:I dont believe that works, since you've already sent text before the header() method. Plus readfile() just dumps the actual file contents, so your code will look like (if the header() command is taken out):

... <td> #$U%I#%U#IO$%U@#%BINARY_FROM_JPEG_FILE@#$J%J@#% </td> ....

I'm gonna try the .htaccess code now ... hope that works.
You can call an image binary from anywhere in your script (even if headers have already been sent). You just do this:
getimage.php

Code: Select all

$file = $image."jpg";
header('content-type: image/jpeg'); 
echo readfile($file);
yourscript.php

Code: Select all

<html> 
<body> 
<table> <tr> 
<td> Some text </td> 
<td>

<img src="getimage.php?image=yourfile">

</td></tr></table> 
</html>
I use includes to get images from a database, so the above should work (I didn't test it though :) )
The above works, I like to use it for counter scripts when I have to pass on vavascript data(browser, screen size, etc.) to a php script. I make javascript make a <img src="track.php?browser=***&height=***, etc."> then the php script inserts the data into a MySQL database then returns a single pixel for the image, its VERY handy.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

Code: Select all

$file_ref = '/root/path/' .$_GET['file_ref'];
if(file_exists($file_ref))
	{
	$tipe = getimagesize($file_ref);
	$header_ext = ($tipe[2] < 4) ? ($tipe[2] < 3) ? ($tipe[2] < 2) ? ($tipe[2] < 1) ? NULL : 'gif' : 'jpeg' : 'png' : NULL;
	if($header_ext !== NULL)
		{
		header("Content-type: image/" .$header_ext);
		readfile($file_ref);
		}
	}
that should work for all allowable image types if called by
<img src="that_script.php?img_ref=image_name.image_ext" />
Post Reply