将图像发送到SQL数据库

时间:2016-11-07 17:30:56

标签: php sql

我正在开发一个Android应用程序,我从相机拍摄照片并将其发送到服务器。图像作为base64格式的编码字符串从应用程序发送。我可以轻松地将照片保存在本地服务器上,但我无法将其保存在数据库中。为什么呢?

这是我的PHP代码:

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

$ encoded_string是图像的base64,$ decoding_string正好是图像。可能是什么原因导致我将数据库发送到数据库时,image列为空?可能是这种&#39;图像的blob类型之间的错误输入。发送照片的列和.jpg类型?

1 个答案:

答案 0 :(得分:0)

将解码后的base64字符串发送到SQL表

中的BLOB数据类型列