无法上传使用笨base64编码的图像(Unable to upload a base64 encod

2019-06-26 10:35发布

我要上传我从Android应用程序接收base64编码的图像。 我使用的PHP框架CodeIgniter的。 同时通过论坛搜索,在这个环节的问题如何上传笨base64encoded图像是和我一样,但解决的办法有没有为我工作。

这里是我写的代码:

private function _save_image() {
    $image = base64_decode($_POST['imageString']);
    #setting the configuration values for saving the image
    $config['upload_path'] = FCPATH . 'path_to_image_folder';
    $config['file_name'] = 'my_image'.$_POST['imageType'];
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '2048';
    $config['remove_spaces'] = TRUE;
    $config['encrypt_name'] = TRUE;


    $this->load->library('upload', $config);
    if($this->upload->do_upload($image)) {
        $arr_image_info = $this->upload->data();
        return ($arr_image_info['full_path']);
    }
    else {
        echo $this->upload->display_errors();
        die();
    }
}

我收到“您没有选择要上传的文件”

谢谢你的时间。

Answer 1:

错误发生,因为笨的上传库将调查$_FILES超全局和搜索你给它的一个指数do_upload()调用。

而且(至少在2.1.2版本),即使你设置了$ _FILES超全局模仿文件的行为上传它不会通过,因为上传库使用is_uploaded_file到exacly检测一种具有超全局篡改的。 你可以跟踪代码系统/库/ upload.php的:134

恐怕你将不得不重新实现大小检查和文件重命名和移动(我会做到这一点),或者你可以修改笨省略那些检查,但它可以使升级后的框架很难。

  1. 在$图像变量的内容保存到一个临时文件,并设置了$_FILES看起来像这样:

      $temp_file_path = tempnam(sys_get_temp_dir(), 'androidtempimage'); // might not work on some systems, specify your temp path if system temp dir is not writeable file_put_contents($temp_file_path, base64_decode($_POST['imageString'])); $image_info = getimagesize($temp_file_path); $_FILES['userfile'] = array( 'name' => uniqid().'.'.preg_replace('!\w+/!', '', $image_info['mime']), 'tmp_name' => $temp_file_path, 'size' => filesize($temp_file_path), 'error' => UPLOAD_ERR_OK, 'type' => $image_info['mime'], ); 
  2. 修改上传库。 您可以使用CodeIgniter的内置扩展本地库的方式 ,并定义My_Upload(或前缀)类,复制-粘贴do_upload功能和更改以下行:

     public function do_upload($field = 'userfile') 

    至:

     public function do_upload($field = 'userfile', $fake_upload = false) 

    和:

     if ( ! is_uploaded_file($_FILES[$field]['tmp_name']) ) 

    至:

     if ( ! is_uploaded_file($_FILES[$field]['tmp_name']) && !$fake_upload ) 

    在您的控制器,请致电与流动参数do_upload():

     $this->upload->do_upload('userfile', true); 


Answer 2:

大家都知道,如果您收到了Base64编码的图像,作为一个字符串,那么你就需要使用文件上传类。

相反,你只需要使用BASE64_DECODE将其解码,然后用FWRITE / file_put_contents保存解码后的数据...

$img = imagecreatefromstring(base64_decode($string)); 
if($img != false) 
{ 
   imagejpeg($img, '/path/to/new/image.jpg'); 
}  

信用: http://board.phpbuilder.com/showthread.php?10359450-RESOLVED-Saving-Base64-image 。



文章来源: Unable to upload a base64 encoded image using Codeigniter