0

I am trying to upload images to database using prepared statement. Whenever I execute the query the fields name and id are inserted correctly in database as expected except the images I am getting 0 bytes in database. I tried uploading multiple images using move_uploaded_file() and it worked but it just didn't with database I tried uploading an image of only size 10kb and didn't work. its keep giving me 0 byte in database.

here is my code

//uploading image to database
function upload_image($last_id, $conn)
{
    $extension = [
        "jpeg",
        "jpg",
        "png",
        "gif",
    ];
    if (isset($_POST["submit"])) {
        //in case of uploading multiple images
        foreach ($_FILES["input"]["tmp_name"] as $key => $tmp_name) {
            $name = basename($_FILES["input"]["name"][$key]);
            $imageFileType = pathinfo($name, PATHINFO_EXTENSION);

            if (in_array($imageFileType, $extension)) {
                $stmt3 = $conn->prepare("INSERT INTO image (id ,name, image) VALUES (?, ?, ?)");
                $image = addslashes(file_get_contents($_FILES["input"]["tmp_name"][$key]));
                $stmt3->bind_param("isb", $last_id, $name, $image);
                $stmt3->execute();
                $stmt3->close();
            }
        }//end loop
    }//end if
}//end function

there is no need to put the html code of the form since it already worked with move_uploaded_file()

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
has19
  • 1,307
  • 16
  • 31
  • Have you checked the value of `$_FILES["input"]["tmp_name"][$key]` to see that it returns a valid file path, and that the image file is present at that location when the script runs? – ADyson Mar 08 '16 at 16:21
  • The other issue is that `addslashes` is intended to be used with strings. You are trying to pass the (binary) contents of an image to it. I'm not quite sure what your intention is with the use of that function. – ADyson Mar 08 '16 at 16:24
  • yes i already tried executing the same code by uploading with the same variable $_FILES["input"]["tmp_name"][$key] using move_uploaded_file() and it worked – has19 Mar 08 '16 at 16:25
  • i actually tried with and without addslashes and both didn't work . i checked other post it have something to do with security i think. i am not quite sure about that – has19 Mar 08 '16 at 16:27
  • To be honest, would you not be better storing the images on disk, and then just storing the path to them in the database? http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay gives you some of the arguments for/against – ADyson Mar 08 '16 at 16:59
  • This example may also help with the original question: http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php . People have used different ways to retrieve the data from the file, which seems to be your issue. – ADyson Mar 08 '16 at 17:00
  • i think i will just stick with the disk storage i guess .thx – has19 Mar 08 '16 at 17:16

1 Answers1

0

i only replaced the type of data of the $image in the bind_param from blob 'b' to string 's' and it works.

$stmt3->bind_param("iss", $last_id, $name, $image);

no idea why it didn't work the other way

has19
  • 1,307
  • 16
  • 31