Android应用程序不向数据库PHP MYSQL发送图片

Android应用程序不向数据库PHP MYSQL发送图片,php,android,mysql,web-hosting,Php,Android,Mysql,Web Hosting,我有一个问题,因为图片没有发送到我的数据库。我使用了不同的PHP文件,不再解码图片,一切正常,所有结果都显示在我的数据库中,但当我尝试连接到该文件时,它不起作用。这是无法正常工作的php: <?php header('Content-type : bitmap; charset=utf-8'); if(isset($_POST["encoded_string"])){ $username = $_POST["username"]; $description = $_POST["des

我有一个问题,因为图片没有发送到我的数据库。我使用了不同的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);
}   
}

?>
试试这个:

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

很不清楚什么有效,什么无效。非常不清楚您想要什么。@greenapps好的,第二个代码将所有数据传递到数据库,但图像是base64格式的,所以有很多字符,并且运行缓慢。我想做的是能够使用第一个代码,但它不会将base64解码为我发送的实际图像,并且在数据库或服务器的文件夹中不会显示任何结果。@greenapps我希望第一个代码能正常工作。那么什么不能正常工作呢。显然它已经解码了。那么它保存到文件中的是什么呢?您是否检查了
$is_writed
?你真的用了哪一个
$path
?你是个伟人,非常感谢!但我之前的代码中的问题出在哪里?@D.B.我很乐意!可能是您的$path声明有问题(我不确定)。否则我看不到任何错误。可能是路径导致了问题,我也不确定,无论如何,谢谢!
$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";
}