954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

displaying uploaded image

Hi,

I am uploading images in a form. But it only sends the images in a folder 'upload' and send only the file name to the database. How do I send the image to the database and retrieve it for display.

chitra1
Newbie Poster
24 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 

you are doing fine. Don't store the actual image in your database. To display you can do something like this:

<?php
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        //connect to the db
        $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
 
        // select our database
        mysql_select_db("database_name") or die(mysql_error());
 
        // get the image from the db
        $sql = "SELECT image FROM TABLE WHERE image_id=0";
 
        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
 
        // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);
 
        // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>
Vai
Junior Poster in Training
75 posts since Jan 2008
Reputation Points: 12
Solved Threads: 5
 

Only think I will point out,

Is that the uploaded image when stored would have to be converted to jpeg for that to exclusively work. When working with files you have the option of giving the uploaded images specific filenames and then rendering them through the use of functions such as imagecreatefromjpeg and imagejpeg. What you might want to do instead of using files and expanding on the database idea, would be to store the mime-type, rather then returning each image as an apparent jpeg.

iFuseDan
Newbie Poster
4 posts since Jul 2006
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You