0

I am trying to store the image in database using the blob datatype.

but my program was not storing the image in database.

code:

form.php:

<form action="upload.php" method="post" enctype="multipart/form-data">
 File Name<input type="file" name="image" /><br />
 <input type="submit"  value="Upload" />
</form>

upload.php:

<?php
require_once('connection.php');

if(isset($_POST['submit'])){
    $image  =   addslashes(file_get_contents($_FILES[image]['tmp_name']));
    $query  =   "INSERT INTO images ('image') VALUES('".$image."')";
    mysql_query($query) or die(mysql_error());
    echo "Image id is ".mysql_insert_id();
    echo "Image id is ".mysql_insert_id();
}
?>

please resolve my problem..

6
  • Use backticks on image not single quotes. Any error codes ? Commented Apr 17, 2014 at 12:19
  • 1
    this site is not on resolving problems. it's on answering questions Commented Apr 17, 2014 at 12:19
  • 1
    Please don't store images as BLOBs, just don't. Commented Apr 17, 2014 at 12:29
  • why?if we store the image as blob type what is the problem. i know performance,any other problems are there,please tell me. Commented Apr 17, 2014 at 12:31
  • @cake Filesystem is just enough. Think if you want to convert, scale, crop...or move assets to a different server/storage system [e.g. cloud]. Commented Apr 17, 2014 at 12:34

4 Answers 4

3

A BLOB can store 65535 bytes maximum. If you need more consider using a MEDIUMBLOB for 16777215 bytes or a LONGBLOB for 4294967295 bytes.

Look at Storage Requirements for String Types.

My suggestion is use LONGBLOB instead of BLOB. Hope it will works.

Sign up to request clarification or add additional context in comments.

7 Comments

thank u for your valuable suggestion. but that actual problem is not that. i think any think is fine for me for my requirement.i am giving long blob also not working
The best approach is to store the image on your filesystem and then reference its location via MySQL. Storing images in MySQL is not recommended. Why put an extra unnecessary strain on your database? All of the large websites (Facebook etc) store their images on a filesystem.
i know about file system and performance but my client ask blob datatype only. ok i understood what you said. but my actual question was storing the image in database is fine. its blob or long blob anything is fine.
I hope this vide tutorial will help you for your requirement : youtube.com/watch?v=kPGxWaIhLmk .. its a video tutorial showing you how to store images inside a database using the BLOB data type. The showing you how to show that image on a page.
if any other example also give me please
|
1

try to use the below code.

    $image  =   addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $query  =   "INSERT INTO images (image) VALUES('".$image."')";

i think this time its work fine...

1 Comment

this is fine for my requirement can give other ones also
0

Try using backticks on fieldName

$image  =   addslashes(file_get_contents($_FILES['image']['tmp_name']));

Comments

0

try this one.this is also another approach for storing the blob image.

code:

    $image= $_FILES["image"];
    $image= mysql_real_escape_string("$image");
    $query = "INSERT INTO images (image) VALUES('".$image."')";
    mysql_query($query) or die(mysql_error());
    echo "Image id is ".mysql_insert_id();

1 Comment

this is also fine for another example for my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.