Page 1 of 1

Photo url

Posted: Fri Dec 23, 2005 4:33 am
by MaNiAC
Hi,

I'm create a site, where users can upload images (already works). They can view their images.. but I want to make sure they can't use the direct link to the image. So when the image is stored in 'files/image' (note how it does not have an extension), I'd like to display it on the page, without having that ('files/image') in the link, so they can not link to that image on other sites.. or try to steal other users images.

All the errors are currently 'invalid file id', because it does not tell a user (if he tries to hack something) what he's doing wrong.
id = the id of the photo, stored in the database. 'files/$id' is the location of the photo
uid = the id of the user who uploaded to photo
original = the original name of the photo

Code: Select all

if($action == 'view'){
 if( !isset($id) ){
   $error = "Invalid file id!";
 } else {
   $res = mysql_query("SELECT uid, original FROM files WHERE name = '".$id."'");
   if( $u = mysql_fetch_array($res) ){
     if($u['uid'] != $thisuser['id']){
       $error = "Invalid file id!";
     } else {
       //show the image here
       $url = "files/".$id;
     }
   } else {
      $error = "Invalid file id!";
   }
 }
Can somebody help me finish this script? It should show an image where the '//show the image here' text is.
I managed to get the whole image in a var by adding this under url = bla bla

Code: Select all

$image = file($url);
But I cannot show it on the page as an image.


Any other script to make the url invalid would also do the trick :)

Thanx in advance,

MaNiAC

Posted: Fri Dec 23, 2005 4:38 am
by onion2k

Code: Select all

header("Content-type: image/jpeg");
readfile($url);

Posted: Fri Dec 23, 2005 4:45 am
by MaNiAC
I want to show the image in the middle of a page. Therefor, I cannot modify the headers.

Thx anyway, and sorry for not being clear about what I wanted in the first post :)

Posted: Fri Dec 23, 2005 5:06 am
by onion2k
MaNiAC wrote:I want to show the image in the middle of a page. Therefor, I cannot modify the headers.

Thx anyway, and sorry for not being clear about what I wanted in the first post :)
You can't embed image data in the middle of an HTML page*. You need to make 2 scripts. One that generates an HTML page with your layout, and one that outputs your image. In the HTML one you'll have <img src="image.php"> .. then image.php should contain what I posted before .. complete with headers.

* Ok, you can, but it'll only work in IE.