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

编程入门 行业动态 更新时间:2024-10-26 12:32:41
本文介绍了无法使用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.

  • 将$ 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编码的图像

    本文发布于:2023-11-26 19:30:36,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1634898.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:图像   上传   Codeigniter

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!