Photo url

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

Post Reply
MaNiAC
Forum Newbie
Posts: 20
Joined: Fri Dec 23, 2005 4:20 am

Photo url

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

header("Content-type: image/jpeg");
readfile($url);
MaNiAC
Forum Newbie
Posts: 20
Joined: Fri Dec 23, 2005 4:20 am

Post 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 :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply