user1494459
user1494459

Reputation:

Unable to show Images stored in mySQL using PHP

I am trying to store images in mySQL database and then displaying on the other page. I have this function to store images in mySQL.

 function upload() {
        include "databaseConnection.php";
        $maxsize = 10000000; //set to approx 10 MB
        //check associated error code
        if ($_FILES['userfile']['error'] == UPLOAD_ERR_OK) {

            //check whether file is uploaded with HTTP POST
            if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

                //checks size of uploaded image on server side
                if ($_FILES['userfile']['size'] < $maxsize) {


                    // prepare the image for insertion
                    $imgData = addslashes(file_get_contents($_FILES['userfile']['tmp_name']));

                    // put the image in the db...
                    // database connection
                    mysql_connect($host, $user, $pass) OR DIE(mysql_error());

                    // select the db
                    mysql_select_db($db) OR DIE("Unable to select db" . mysql_error());

                    // our sql query
                    $sql = "INSERT INTO carsinfo
                (carName, carPicture)
                VALUES
                ('{$_FILES['userfile']['name']}', '{$imgData}');";

                    // insert the image
                    mysql_query($sql) or die("Error in Query: " . mysql_error());
                    $msg = '<p>Image successfully saved in database with id =' . mysql_insert_id() . ' </p>';
                } else {
                    // if the file is not less than the maximum allowed, print an error
                    $msg = '<div>File exceeds the Maximum File limit</div>
            <div>Maximum File limit is ' . $maxsize . ' bytes</div>
            <div>File ' . $_FILES['userfile']['name'] . ' is ' . $_FILES['userfile']['size'] .
                            ' bytes</div><hr />';
                }
            }
            else
                $msg = "File not uploaded successfully.";
        }
        else {
            $msg = file_upload_error_message($_FILES['userfile']['error']);
        }
        return $msg;
    }

And this code to show iamges.

    <?php
    include "databaseConnection.php";
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    //connect to the db
    $link = mysql_connect("$host", "$user", "$pass") or die("Could not connect: " . mysql_error());

    // select our database
    mysql_select_db("$db") or die(mysql_error());
    // get the image from the db
    $sql = "SELECT carPicture FROM carsinfo;";
    // the result of the query
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
    $row = mysql_fetch_assoc($result);
    // set the header for the image
    echo $row['carPicture'];
    header("Content-type: image/jpeg");
    // close the db link
    mysql_close($link);
    ?>

When this code is run, nothing is shown on the page, even if I write some HTML inside this code, empty page is shown.

Upvotes: 0

Views: 277

Answers (2)

Deniz
Deniz

Reputation: 1

do not write include databaseConnection.php

nothing include anything.

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44864

I assume you are saving the image content as blob and if your sql is returning the correct data then you can display as

header("Content-type: image/jpeg");
echo $row['carPicture'];

You need to add the header first before the image.

or

echo '<img src="data:image/jpeg;base64,' . base64_encode($row['carPicture']) . '">';

Upvotes: 2

Related Questions