Mysql upload image[mediumblob]

nice to see you again…pardon my english–

I have a form and I’m trying to upload a image into mediumblob field in mysql.
I have five field…id, title, text, data, image.
All the field used method $_POST
All fields are ok except ‘image’.

if i upload the image used phpmyadmin evrything is OK. The medium blob field is full.
if I upload the image whit the form evrything is wrong. The medium blob field is empty --there is no image.

thanks too much for the answare.

Uploaded files are not in $_POST, but $_FILES. see

the guide is too difficulty for me, you can explain to me a easy way, thanks

This guide is almost ready for copy-paste. And it also includes the security considerations to make.

sorry i don’t need to check all parameter, i don’t know really, i want only upload the image whit a form…you can help me?

this is code php:


    include "config.php";


   $titolo = $_POST["titolo"];

   $testo = $_POST["testo"];

   $file_immagine = $_FILES['img']['tmp_name'];

   $sql = "INSERT INTO articoli (titolo, testo, img)
   VALUES ('$titolo', '$testo', '$file_immagine')";

   if(mysqli_query($conn, $sql)){
    echo "New record created successfully<br>";
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);


this is the form:

   <form method="post" action="" 
     <input type="text" name="titolo">
      <textarea type="text" name="testo" cols="80" rows="20"></textarea>
      <h2>Carica un immagine</h2>
     <input type="file" name="file_immagine" size="100">   
     <br /><br />
     <input class="btnform" type="submit" value="invia al blog!">

you can take a look? Thanks.

All you’re doing here

 $file_immagine = $_FILES['img']['tmp_name'];

   $sql = "INSERT INTO articoli (titolo, testo, img)
   VALUES ('$titolo', '$testo', '$file_immagine')";

is inserting the temporary name of the file into your database table, not the contents of the file itself.

How can i use the array $_FILE…you can correct my code? I don’t know really…

you can show me? Thanks.

Well, I’ve never tried it myself but I think something like this might start the job off:

$file_immagine = $_FILES['img']['tmp_name'];
$file_image_contents = file_get_contents($file_immagine); // load image file
   $sql = "INSERT INTO articoli (titolo, testo, img)
   VALUES ('$titolo', '$testo', '$file_image_contents')";

BUT. I can’t imagine that will work without somehow encoding the binary image data. I’ve read about using addslashes() after loading the image contents, but I’m sure someone else can comment on whether that’s still a current way to do things.

Another approach suggested in an older thread on here is to modify the query:

 $file_immagine = $_FILES['img']['tmp_name'];
   $sql = "INSERT INTO articoli (titolo, testo, img)
   VALUES ('$titolo', '$testo', LOAD_FILE('$file_immagine'))";

In the same thread there is discussion on the pros and cons of storing the image data directly inside the database table. Inserting and then retrieving a blob data from mysql database using php - #7 by felgall

nothing to do, all methods do not seem to work…generates error.
you have any solution…

What error does it generate? Show the latest code you’re using please, and the error it generates.

the first method(create content) the error is a long series of mixed characters.

the second method(LOAD FILE) the error is…empty field in mysql…only this.


For LOAD_FILE, is the field big enough for the file you are trying to upload into it, and are the permissions correct for you to access the file?

For the other method, did you have a look at the addslashes() function that I mentioned you might need?

1 Like

thank you…you’ve been very helpful…I will write again bye bye.

Why are you storing the image in the database table?
I save the file into a files directory and store the filename in the table. Much cleaner and easier.

That article I linked to earlier on also talked about the good and bad points of doing that. Of course, storing the image in the table means that, as long as you backup the table, you don’t lose any images in the event of a system problem. Of course, having a proper backup strategy that also backs up the uploaded images folder also solves that issue. The down-side, of course, is having much larger database tables.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.