Search code examples
phpimagemime-types

Loading an image through PHP


I'm trying to load an image through PHP, but I don't know how. The filename is stored in the database, such as image.jpg

if($_GET['image']){
    // Client requesting image, so retrieve it from DB
    $id = mysql_real_escape_string($_GET['image']);
    $sql = "SELECT * FROM $tbl_name WHERE id = '$id' LIMIT 1";
}

The client needs to request an image like so

http://example.com/index.php?image=1

And then it should return the image, so it can be embedded like this:

<img src="http://example.com/index.php?image=1" alt="" />

Is this possible?


Solution

  • $img = 'path/to/image.jpg';
    header('Content-Type: image/jpeg');
    readfile($img);
    

    just tested it