0

I am using PHP and Mysql to store image in MySQL Database. The image is getting stored in Blob field called B_Image. However, when i try to retrieve the image, it shows characters instead of the image.

I know i still need to write some code to validate certain details. But first i wanted to try and see if it would upload and retrieve the image.

Could you please help me in displaying the image.

My UploadImage.php code:

include"config.php"; //code to connect to my database

     $file =$_FILES['txtBookCover']['tmp_name'];

$Title = $_POST[txtBookName];
        $Author = $_POST[txtBookAuthor];
        $ISBN = $_POST[txtISBN];
        $Type = $_POST[lstGenre];

         $image = addslashes(file_get_contents ($_FILES['txtBookCover']['tmp_name']));
        echo $image_name = addslashes($_FILES['txtBookCover']['name']);
         $image_size = getimagesize($_FILES['txtBookCover']['tmp_name']);

     $SqlStatement= "INSERT INTO Books(B_Title, B_Author, B_ISBN, B_Image, B_ImageName) VALUES('$Title', '$Author', '$ISBN','$image','$image_name')";
if (mysql_query($SqlStatement)) {

    print "Image has been uploaded!";

    }
    else
    {

    print "Error uploading image";
    }

My DisplayImg.php code:

    include"config.php";
$SqlStatement="select B_Image,B_Author,B_ID FROM Books where B_Author='ff'";
$result=mysql_query($SqlStatement);

if (mysql_query($SqlStatement)) {


    echo"hellooo";

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo 
         "Author : {$row['B_Author']} <br>" . 
         "ID : {$row['B_ID']} <br><br>";

         echo "<br>";


         header('Content-type: image/jpg');
         echo "Image: {$row['B_Image']}<br>";        
} 

}

else

{
echo "error";   
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
Mumchii
  • 11
  • 3

1 Answers1

1

I think it is a bad idea storing images in the database. Usually what we only store on the database is the path of the image, and the image itself is stored on the server. Good Luck. :)

  • In this instance, it's probably fine; a good idea, even!. The images are likely to be very small, and certainly well under the 250k at which point this idea appears to become inefficient. – Strawberry May 30 '15 at 10:20
  • Oh, I see, But yeah you're right. If we're talking about millions or billions of images here, then it is not very efficient to store it in the database. Thanks for pointing that out @Strawberry. – R Judd Celis May 30 '15 at 10:23
  • No. The quantity of images is probably fairly irrelevant. It's the size of them that counts, but as long as they are under 20k, say, then it should be fine. – Strawberry May 30 '15 at 10:30
  • Thanks everyone! I will try to store them on server and save the path in the database! – Mumchii Jun 01 '15 at 14:02
  • I tried to save the photos under the folder 'photos' on the server. It says "Image has been uploaded", however the folder is empty. Could you please help me: – Mumchii Jun 01 '15 at 16:32
  • alert('Please select an image') "; exit(); } else { if(move_uploaded_file($image_tmp_name,$dir."/".$name)) { echo "The file ". $name. " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } } ?> – Mumchii Jun 01 '15 at 16:39