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

时间:2018-02-11 16:55:25

标签: php android mysql web-hosting

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

public Camera MyCamera; // The camera you want to follow the GameObject
public GameObject ObjectToFollow; // What you want to follow

private Vector3 CameraPos; // Variable that contains the Cameras x,y,z position

void Start()
{
    CameraPos = MyCamera.transform.position; // stores the Camera's position in the variable
}

// Update is called once per frame
void Update () {
    CameraPos.x = ObjectToFollow.transform.position.x; // Change The X position on the camera variable to be the same as the ObjectToFollow X position
    MyCamera.transform.position = CameraPos; // Moves the Camera to the new position
}

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

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

?>

是否因为我必须更改FTP设置而被遗忘?

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

1 个答案:

答案 0 :(得分:1)

试试这个:

$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";
}