PHP: Image simple question

Discussion in 'PHP' started by born2hack, Oct 16, 2008.

  1. #1
    Its simple striaght, I dont want to actually use any image function, I just want that users CAN view my images, but can not get the actuall image url/address, So its like this, I have the urls of image stored ina database with an id column in front of it. when user opens:

    display.php?id=187

    The database finds that 187 is /images/blabla.jpg

    The page displays the same image.

    I searched alot but no use....

    Thanks in advance
     
    born2hack, Oct 16, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    if your images filename are driven from the database, use a foreign key, with this you can hide your directory..
     
    bartolay13, Oct 16, 2008 IP
  3. imphpguru

    imphpguru Active Member

    Messages:
    439
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Hi,
    The code is simple
    
    $imgid = $_GET['id'];
    
    $img_location = mysql_fetch_array(mysql_query("SELECT path from imagetable where image_id='{$imgid}'"));
    $img_location = $img_location[0]['path']; // GETS THE PATH
    
    $base_path = 'http://www.url.com/images_base_path';
    
    header("location:{$base_path}/{$img_location}");
    
    PHP:
    This should redirect the browser to the image file... So if you have this named as image.php... then it will redirect it to the right image. You can add conditions to check if it is proper or not.

    Thanks

    imphpguru
     
    imphpguru, Oct 16, 2008 IP
  4. born2hack

    born2hack Banned

    Messages:
    294
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That would give away the image's LOCATION to the user, and thats what i want to prevent, anyways thanks for ya all's help but I figured out myself, :D
     
    born2hack, Oct 17, 2008 IP
  5. imphpguru

    imphpguru Active Member

    Messages:
    439
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #5
    hi,

    great to know that you found it for yourself... it will show the image path only when you go through to view image option. else you can also use file_get_contents something like this...

    
    $imgid = $_GET['id'];
    
    $img_location = mysql_fetch_array(mysql_query("SELECT path from imagetable where image_id='{$imgid}'"));
    $img_location = $img_location[0]['path']; // GETS THE PATH
    
    $base_path = 'http://www.url.com/images_base_path';
    
    echo file_get_contents("{$base_path}/{$img_location}");
    
    PHP:
    Hope you find this one exactly matching with what you needed :)
     
    imphpguru, Oct 23, 2008 IP
  6. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    use that if you don't want SQL injections

    edit: actually, half of the code is bogus.

    here's a better version:
    
    $imgid = $_GET['id'];
    
    list($img_location) = mysql_fetch_array(mysql_query("SELECT path from imagetable where image_id='".intval($imgid)."' LIMIT 1"));
    if($img_location) {
    $base_path = 'images_folder';
    $ending = substr($img_location,strrpos($img_location,".")+1);
    header("Content-type: image/".$ending);
    echo file_get_contents("{$base_path}/{$img_location}");
    } else {
    //display error in image form here maybe?
    }
    
    PHP:
    whoever wrote the original should be ashamed of themselves. Bad code.
     
    Kyosys, Oct 24, 2008 IP
  7. salahsoftware

    salahsoftware Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    echo file_get_contents("file.jpg");

    this will display the image without its link.
     
    salahsoftware, Oct 24, 2008 IP
  8. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yes actually. It's stupid to use http:// if it's on the same server
     
    Kyosys, Oct 24, 2008 IP
  9. imphpguru

    imphpguru Active Member

    Messages:
    439
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #9
    Hi,
    The script was just an example of how it can be done.. not the exact script.

    Thanks

    dotphp
     
    imphpguru, Oct 24, 2008 IP