将Base64编码的图像上传到PHP服务器 [英] Uploading a Base64 encoded Image to PHP server

查看:316
本文介绍了将Base64编码的图像上传到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to upload a Base64 encoded .Png file to a PHP server from an Android application.

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

Below code doesn't return anything in the response. What am I doing wrong?

我正在发送Base64编码的字符串和文件名. (例如:"sign1234.png")

I'm sending the Base64 encoded string and the name of the file. (ex:"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'].我编写了仅使用这两个给定数据的脚本.我没有测试我的脚本,但它可以为您解决问题.

It seems you are trying to get data from post but also get image data from $_FILES but if i read your comment correct you only sent data from $_POST['image'] and $_POST['name']. I have written a script that use only those 2 given data. I didn't test my script but it should do the trick for you.

更改

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

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

查看您的服务器详细信息.

to your server details.

代码:

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

这篇关于将Base64编码的图像上传到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆