0

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:

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

if(isset($_POST["encoded_string"])){

$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);
fclose($file);

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) ;

    if($result){
        echo "success";
    }else{
        echo "failed";
    }

    mysqli_close($connection);
}
}
?>

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?

  • Given the SQL injection vulnerability, you could be executing pretty much anything here. What is the actual SQL query that's failing? In what way does it fail? (Note: You're not checking for errors from the database.) – David Nov 07 '16 at 17:33
  • There is a way to insert an image to a db. Have a look at longblobs – Blueblazer172 Nov 07 '16 at 17:33
  • Why would you save it in the database? Just save the path to the file in the database. – Styphon Nov 07 '16 at 17:33
  • It's not a database for a public app, but just for a local company :) I am already saving the path but I have to remove it –  Nov 07 '16 at 17:35
  • @xzayt: Whether the application is public or internal doesn't change the need to prevent and monitor errors. Errors can happen on internal apps. – David Nov 07 '16 at 17:36
  • @David The SQL query does not fail. All the data is successfully sent and saved, but my image column is always null –  Nov 07 '16 at 17:36
  • @David could you please tell something more specific? I am new to php. How could I monitor the errors? –  Nov 07 '16 at 17:37
  • 1
    what is the datatype of your `image` column – vishal Nov 07 '16 at 17:38
  • @vishal it's BLOB –  Nov 07 '16 at 17:38
  • @xzayt: Take a look at `mysqli_error()`. If the record is being saved but without that value, then check what that runtime value actually is. Currently this is all guesswork without any actual debugging. – David Nov 07 '16 at 17:39
  • @xzayt did you find an error ? – vishal Nov 07 '16 at 17:43
  • @vishal I haven't found any errors –  Nov 07 '16 at 17:44
  • have you followed this http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php ? – vishal Nov 07 '16 at 17:51
  • I've searched the internet a bit and found that I should send the images to the database as binary objects, but anyway I did not understand how to do it. –  Nov 07 '16 at 20:41

1 Answers1

0

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