sheepiiHD sheepiiHD - 1 year ago 43
PHP Question

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;
$errors[] = "Please add an image.";

The problem I'm getting is that
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
& the file I'm uploading is gif.

Answer Source

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.