简体   繁体   English

无法使用Codeigniter上传base64编码的图像

[英]Unable to upload a base64 encoded image using Codeigniter

I have to upload a base64 encoded image that i am receiving from android application. 我必须上传从Android应用程序接收到的base64编码图像。 I am using php codeigniter framework. 我正在使用php codeigniter框架。 While searching through the forum, the question at this link How to upload base64encoded image in codeigniter is same as mine, but the solution there is not working for me. 在论坛中搜索时,此链接上的问题如何在codeigniter中上传base64encoded图像与我的相同,但那里的解决方案对我不起作用。

Here is the code that i have written: 这是我编写的代码:

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

I am getting "you did not select a file to upload" 我收到“您未选择要上传的文件”

Thanks for your time. 谢谢你的时间。

The error is occurring because codeigniter's upload library will look into the $_FILES superglobal to and search for a index you give it at the do_upload() call. 发生错误是因为codeigniter的上载库将查找$_FILES超全局对象,并搜索在do_upload()调用中为其提供的索引。

Furthermore (at least in version 2.1.2) even if you would set up the $_FILES superglobal to mimic the behaviour of a file upload it wouldn't pass because the upload library uses is_uploaded_file to detect exacly that kind of tampering with superglobals. 此外(至少在版本2.1.2中),即使您设置$ _FILES超全局变量来模仿文件上传的行为,它也不会通过,因为上载库使用is_uploaded_file来检测这种对超全局变量的篡改。 You can trace the code in system/libraries/Upload.php:134 您可以在system / libraries / Upload.php:134中跟踪代码

I'm afraid that you will have to re-implement size checking and file renaming and moving (I would do this) or you can modify codeigniter to omit that check, but it could make upgrading the framework later difficult. 恐怕您将不得不重新实现大小检查以及文件重命名和移动(我会这样做),或者您可以修改codeigniter来忽略该检查,但是这可能会使以后升级框架变得困难。

  1. Save the $image variable's content to a temporary file, and set up the $_FILES to look like this: 将$ image变量的内容保存到一个临时文件中,并设置$_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. Modify the upload library. 修改上传库。 You can use codeigniter's built in way of Extending Native Libraries , and define a My_Upload (or your prefix) class, copy-paste the do_upload function and change the following lines: 您可以使用codeigniter的扩展本机库的内置方式 ,定义My_Upload(或您的前缀)类,复制粘贴do_upload函数并更改以下行:

     public function do_upload($field = 'userfile') 

    to: 至:

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

    and the: 和:

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

    to: 至:

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

    and in your controller, call do_upload() with the flowing parameters: 在您的控制器中,使用以下参数调用do_upload():

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

You are aware, that if you are receiving an Base64 encoded image, as a string, then you do not need to use the Upload class. 您知道,如果您以字符串的形式接收Base64编码的图像,则无需使用Upload类。

Instead, you just need to decode it using base64_decode and then use fwrite/file_put_contents to save the decoded data... 相反,您只需要使用base64_decode对其进行解码,然后使用fwrite / file_put_contents保存已解码的数据...

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

Credit: http://board.phpbuilder.com/showthread.php?10359450-RESOLVED-Saving-Base64-image . 图片来源: http : //board.phpbuilder.com/showthread.php?10359450-RESOLVED-Saving-Base64-image

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

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