简体   繁体   中英

Unable to upload a base64 encoded image using Codeigniter

I have to upload a base64 encoded image that i am receiving from android application. I am using php codeigniter framework. 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.

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.

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. You can trace the code in 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.

  1. Save the $image variable's content to a temporary file, and set up the $_FILES to look like this:

      $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:

     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:

     $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.

Instead, you just need to decode it using base64_decode and then use fwrite/file_put_contents to save the decoded data...

$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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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