繁体   English   中英

将Base64编码的图像上载到PHP服务器

[英]Uploading a Base64 encoded Image to PHP server

我正在尝试从Android应用程序将Base64编码的.Png文件上传到PHP服务器。

下面的代码不会在响应中返回任何内容。 我究竟做错了什么?

我正在发送Base64编码的字符串和文件的名称。 (比如: “sign1234.png”)

<?php

if(isset($_POST['image']) && isset($_POST['name']){

$image = $_POST['image'];
$name = $_POST['name'];

$png = base64_to_jpeg($image,$name);

$target = 'uploads/'.$name;
$result = move_uploaded_file( $_FILES['$png']['tmp_name'], $target);

if($result){
        $response["success"] = 1;
        $response["message"] = "Upload Successful.";
        echo json_encode($response);
}else{
    $response["success"] = 0;
    $response["message"] = "Server error. Could not upload.";
    echo json_encode($response);
}

}

function base64_to_jpeg($base64_string, $output_file) {
    $ifp = fopen($output_file, "wb"); 

$data = explode(',', $base64_string);

fwrite($ifp, base64_decode($data[1])); 
fclose($ifp); 

return $output_file; 
}
?>

看来你正试图从帖子中获取数据,但也从$ _FILES获取图像数据但是如果我读了你的评论正确你只发送了来自$ _POST ['image']和$ _POST ['name']的数据。 我编写了一个只使用这两个给定数据的脚本。 我没有测试我的脚本,但它应该为你做的伎俩。

更改

private $ save_path ='serverpath / to / image / folder /';

到你的服务器细节。

码:

<?php

class image{

    private $save_path = 'serverpath/to/image/folder/';
    private $image_string = '';
    private $image_name = '';
    private $image;
    private $response = array();

    public $loaded = false;

    public function __construct(){
        $this->response = array(
            'success' => 0,
            'message' => 'unknown error.'
        );
        $this->image_name = filter_input(INPUT_POST, 'name');
        $this->image_string = filter_input(INPUT_POST, 'image');
        if(!empty($this->image_name) && !empty($this->image_string)){
            $this->loaded = true;
        }
    }

    public function save(){
        if(!empty($this->image_name) && !empty($this->image_string)){
            return $this->progress();
        }
        else{
            $this->response['message'] = 'Error. Not all required infor is given.';
            return $this->response;
        }
    }

    private function progress(){
        $imgarr = explode(',', $this->image_string);
        if(!isset($imgarr[1])){
            $this->response['message'] = 'Error on post data image. String is not the expected string.';
            return $this->response;
        }
        $this->image = base64_decode($imgarr[1]);
        if(!is_null($this->image)){
            $file = $this->save_path . $this->image_name;
            if(file_exists($file)){
                $this->response['message'] = 'Image already exists on server.';
                return $this->response;
            }
            if(file_put_contents($file, $this->image) !== false){
                $this->response['error'] = 1;
                $this->response['message'] = 'Image saved to server';
                return $this->response;
            }
            else{
                $this->response['message'] = 'Error writing file to disk';
                return $this->response;
            }
        }
        else{
            $this->response['message'] = 'Error decoding base64 string.';
            return $this->response;
        }
    }
}

$img = new image();
if($img->loaded){
    $result = $img->save();
    echo json_encode($result);
}
else{
    $result = array(
        'success' => 0,
        'message' => 'Not all post data given'
    );
    echo json_encode($result);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM