For one my application, I need to keep image files in database table. I am using AJAX to send the multi-part form data and can view the image file details on action page.
On action page, base64 encoding is working and showing the correct encoded data. Also, I saw the encoded data is showing on UPDATE SQL query by echoing the SQL statement.
But while trying to execute the SQL query statement then every time its only inserting data:image/;base64,
and ignoring the base64 encoded full data.
When I submit the form without using AJAX then the system update the database table with base64 encoded data.
Sample code: JavaScript:
$(document).on("change", "#bird", function() {
var file_data = $("#bird").prop("files")[0];
var form_data = new FormData();
form_data.append("file", file_data)
$.ajax({
type: 'post',
contentType: false,
processData: false,
data: image_data,
url: "/update-bird.php",
dataType: 'json',
cache:false,
success: function(data) {
// Ajax
}
});
});
PHP:
if(isset($_FILES))
{
$path = $_FILES['file']['name'];
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($_FILES['file']['tmp_name']);
$birdpic = "data:image/".pathinfo($path, PATHINFO_EXTENSION).";base64,".base64_encode(file_get_contents($_FILES['file']['tmp_name']));
$updateQ = "UPDATE birds SET picture='$birdpic' WHERE name='Yoy'";
if($dbo->query($updateQ))
echo 1;
else
echo 0;
}
I hope, will get some advice to solve this issue. Thanks in advance!