无法使用Codeigniter上传base64编码的图像 [英] Unable to upload a base64 encoded image using Codeigniter

查看:99
本文介绍了无法使用Codeigniter上传base64编码的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须上传从Android应用程序接收到的base64编码图像。我正在使用php codeigniter框架。
在论坛中搜索时,此链接上的问题如何上传codeigniter中的base64encoded图像与我的相同,但是那里的解决方案对我不起作用。

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.

这是我编写的代码:

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"

感谢您的时间。

推荐答案

发生错误是因为codeigniter的上载库将查看 $ _ FILES 超级全局化并搜索您在 do_upload()调用中提供的索引。

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.

至少在版本2.1.2中),即使您设置$ _FILES超全局变量来模仿文件上传的行为,该文件也不会通过,因为上传库使用 is_uploaded_file 即可准确地检测到这种对超全局变量的篡改。您可以在system / libraries / Upload.php:134

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

恐怕您将不得不重新实现大小检查以及文件重命名和移动(我会这样做),或者您可以修改codeigniter来忽略该检查,但这可能会使以后升级框架变得困难。

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. 将$ 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'],
 );


  • 修改上传库。您可以使用扩展本机库的方式中内置的codeigniter,并定义My_Upload(或您的前缀)类,复制并粘贴do_upload函数并更改以下行:

  • 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')
    

    至:

    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():

    and in your controller, call do_upload() with the flowing parameters:

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


  • 这篇关于无法使用Codeigniter上传base64编码的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆