xz ayt xz ayt - 11 months ago 55
PHP Question

Send a image to the SQL database

I am working on an android application where I take a photo from the camera and send it to the server. The image is sent from the app as an encoded string in the base64 format. I can easily save the photo on the local server, but I cannot save it in the database. Why?

here is my php code:

header('Content-type : bitmap; charset=utf-8');


$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];
$detail1 = $_POST["detail1"];

$decoded_string = base64_decode($encoded_string);

$path = 'images/'.$image_name;

$file = fopen($path, 'wb');

$is_written = fwrite($file, $decoded_string);

if($is_written > 0) {

$connection = mysqli_connect('localhost', 'root', 'pass','image-test');
$query = "INSERT INTO photos(name,path,detail1,image) values ('$image_name','$path', '$detail1', '$decoded_string');";

$result = mysqli_query($connection, $query) ;

echo "success";
echo "failed";


The $encoded_string is the base64 of the image, and the $decoded_string is properly the image. What may be the cause that when I send it to the database, the image column is null? May it be a mistyping between blob type of the 'image' column and .jpg type of the photo sent?

Answer Source

send the decoded base64 string to the BLOB datatype column in the SQL table