Search code examples
phpsqlimagebase64input-type-file

How to get the image into data that you can save to SQL


if(isset($_POST["imageuploader"])){ //Is submitted
    if ($_FILES['image']['tmp_name']){ //LINE 3
        $data   = file_get_contents($_FILES['image']['tmp_name']);
        $base64 = base64_encode($data);
        $item   = inputSanitize($_POST['item']);


        $updateImage = $odb->prepare("UPDATE `items` SET `image` = ? WHERE `id` = ?;");
        $updateImage->BindValue(1, $base64);
        $updateImage->BindValue(2, $item);
        $success = true;
    }else{
        $errors[] = "Please add an image.";
    }
}

The problem I'm getting is that $_FILES['image']['tmp_name'] is apparently an undefined index:

Notice: Undefined index: image in /var/www/html/staff/imageupdater.php on line 3

However, I have this in my form:

<input type="file" name="image" accept="image/*">

file_uploads = On in my php.ini & the file I'm uploading is gif.


Solution

  • Likely you are missing the enctype in your form tag.,

     <form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
    

    Without that no file is uploaded and the $_FILES is not populated, hence the undefined image index.