How to display images who are outside document root (www) ?
Moderator: General Moderators
-
pootergeist
- Forum Contributor
- Posts: 273
- Joined: Thu Feb 27, 2003 7:22 am
- Location: UK
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);
}
}-
pootergeist
- Forum Contributor
- Posts: 273
- Joined: Thu Feb 27, 2003 7:22 am
- Location: UK
- redhair
- Forum Contributor
- Posts: 300
- Joined: Fri May 30, 2003 4:36 pm
- Location: 53.23N-6.57E
- Contact:
well (thanks a lot for starters), i did as you say, but i get no image.
i writen away your first code away as 'image.php':
then as you pointed out, i call it with <img src="image.php?file_ref=icon.gif" />.
what did i do wrong? ..for the image 'icon.gif' is there in /myacount/phpupl/.
i writen away your first code away as 'image.php':
Code: Select all
<?php
$file_ref = '/usr/local/apache/www/myaccount/phpupl/' .$_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);
}
}
?>what did i do wrong? ..for the image 'icon.gif' is there in /myacount/phpupl/.
- redhair
- Forum Contributor
- Posts: 300
- Joined: Fri May 30, 2003 4:36 pm
- Location: 53.23N-6.57E
- Contact:
I expirimented a little.
To see if it would work at all...
So i set up a dir IN the www root.
/usr/local/apache/www/myaccount/html/test...placed an image in it, and called it using your code...and it works! So...i didnt do anything wrong, and your code is super.
Now that we sorted out that we are cool, what should be the next step in order to make the images be retreived from the location where they should be stored. (/phpupl/) I figure it has to do something with rights on the server. Does anybody know wich rights should be changed?
To see if it would work at all...
So i set up a dir IN the www root.
/usr/local/apache/www/myaccount/html/test...placed an image in it, and called it using your code...and it works! So...i didnt do anything wrong, and your code is super.
Now that we sorted out that we are cool, what should be the next step in order to make the images be retreived from the location where they should be stored. (/phpupl/) I figure it has to do something with rights on the server. Does anybody know wich rights should be changed?
- redhair
- Forum Contributor
- Posts: 300
- Joined: Fri May 30, 2003 4:36 pm
- Location: 53.23N-6.57E
- Contact:
Nope, there is only the first. (..where did you get that second one from?)so the dirs are:/usr/local/apache/www/myacount/phpupl/
and:/usr/local/apache/www/myacount/html/upload ?
And i tried...and tried and read, and read...expiriment this..and that.
But still no go! The image will not show.
Sooo...again, it must(?) be a setting on the server.
'I'm not allowed to read the phpupl directory'?
Because as i mentioned before, using the same script while trying to read a file in document root, does work. (using path:/usr/local/apache/www/myacount/HTML/somdir/)
-
Elantri_X
- Forum Newbie
- Posts: 1
- Joined: Mon Jun 09, 2003 2:18 am
- Location: Sydney, Australia
- Contact:
You cant read files out of your given home directory. If this was possible, you'd have a HUGE security risk when someone uploads a file to read files like .htaccess... this brings me to another solution for you.
The directory you want your pics in to read from can have .htaccess file
.htaccess
put this into the folder you want nobody to access..
The directory you want your pics in to read from can have .htaccess file
.htaccess
Code: Select all
deny from all- redhair
- Forum Contributor
- Posts: 300
- Joined: Fri May 30, 2003 4:36 pm
- Location: 53.23N-6.57E
- Contact:
i guess you didnt read the issue/ question very good.Elantri_X wrote: The directory you want your pics in to read from can have .htaccess file
Never mind..
I have been told that it is not possible.
Goal was to make it as safe as possible.
Only gifs and jpgs would be allowed.
i was planning to store the images in the dir above www root, rename them while they get uploaded (something like 643789478454545784.gif) Store the real name in the database+the new name, and then have it called by some page,....easy as that.
But o to bad...that party won't take place.
I go try the other (poor) option left, namely, to store the images in mysql.