duan1982453 2016-06-05 06:24
浏览 75
已采纳

将Base64编码的图像上载到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?

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; 
}
?>
  • 写回答

1条回答 默认 最新

  • ds0409 2016-06-05 09:46
    关注

    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.

    change

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

    to your server details.

    Code:

    <?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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题