Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...