Android 应用程序没有将图片发送到数据库 PHP MYSQL

Android app doesnt send picture to database PHP MYSQL

我有问题,因为图片没有发送到我的数据库。我使用了不同的 PHP 文件,它不会再次解码图片并且一切正常,所有结果都出现在我的数据库中,但是当我尝试连接到该文件时它不起作用。这是无法正常工作的 php:

<?php

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

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

$username = $_POST["username"];
$description = $_POST["description"];
$encoded_string = $_POST["encoded_string"];

$decoded_string = base64_decode($encoded_string);

$path = 'place on server where I want pictures to be sent' ;

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

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

if($is_written > 0){
    $con = mysqli_connect("localhost", "xx", "xx", "xx");
    $query = "INSERT INTO meals(username, description, image) values('$username', '$description' , '$path');";

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

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

    mysqli_close($con);
}   
}

?>

那个发送详细信息的方式正确,但不是我想要的方式:

<?php
 $con = mysqli_connect("localhost", "xx", "xx", "xx");

$username = $_POST["username"];
$description = $_POST["description"];
$encoded_string = $_POST["encoded_string"];

$statement = mysqli_prepare($con, "INSERT INTO images (username, description, image) 
VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $username, $description, $encoded_string);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;  

echo json_encode($response);


?>

是否因为我必须更改 FTP 设置而引起的?

第二个代码将所有数据传递到数据库,但图像是 base64 格式,因此字符很多,运行速度很慢。我想要做的是能够使用第一个代码,但它不会将 base64 解码为我发送的实际图像,并且它在数据库中和服务器中的文件夹中都没有显示任何结果。

试试这个:

$encoded_string = $_POST["encoded_string"];
$path="uploads"."/".rand()."_".time().".jpeg";  //uploads is folder, file name is composed of random number+underscore+time.jpeg
$upload_url="http://xxx.xx.xx.xx/".$path;
if(file_put_contents($path,base64_decode($encoded_string))){
    //file uploaded, insert $upload_url into database(Type varchar)
}else{
    //echo "file could not uploaded";
}