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

Php 无法使用Codeigniter上载base64编码的图像,php,codeigniter,Php,Codeigniter,我必须上传我从android应用程序接收的base64编码图像。我正在使用php codeigniter框架。 在论坛中搜索时,这个链接上的问题与我的问题相同,但那里的解决方案对我不起作用 以下是我编写的代码: private function _save_image() { $image = base64_decode($_POST['imageString']); #setting the configuration values for saving the image

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

以下是我编写的代码:

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();
    }
}
我收到“您没有选择要上载的文件”


谢谢你抽出时间

之所以发生此错误,是因为codeigniter的上载库将查看
$\u文件
超全局到,并搜索您在
do\u upload()
调用中为其提供的索引

此外(至少在版本2.1.2中),即使您将$_filessuperglobal设置为模仿文件上载的行为,它也不会通过,因为上载库用于检测对superglobal的篡改。您可以在system/libraries/Upload.php:134中跟踪代码

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

  • 将$image变量的内容保存到临时文件中,并将
    $\u文件设置为如下所示:

     $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函数并更改以下行:

    public function do_upload($field = 'userfile')
    
    致:

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

    在控制器中,使用以下参数调用do_upload():

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

  • 您知道,如果您正在接收作为字符串的Base64编码图像,则不需要使用Upload类

    相反,您只需要使用base64_decode对其进行解码,然后使用fwrite/file_put_contents保存解码后的数据

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

    信用证:。

    您好,谢谢您的回复。我按照您的指导原则进行了更改,但我发现以下错误:1。第86行的未定义索引是“$this->file\u name=$this->\u prep\u filename($\u FILES[$field]['name']);”在do_upload函数中,我复制了MY_upload类。2.不允许您尝试上载的文件类型。这是打印数组([userfile]=>Array([tmp\u name]=>C:\Windows\Temp\and15BC.tmp[size]=>354129[error]=>0[type]=>image/png))的输出,`我忘记了
    $\u文件
    中的'name'键,将示例更新为,这应该可以修复这两个错误。Hi complex857,图像上传功能在我的系统上运行良好,这就是Windows7,wamp。但是今天我把它上传到了服务器上,代码在那里不起作用。我收到“您没有选择要上载的文件”这条消息。我回显了file_put_contents()函数的输出,根据文档,我得到了字节数计数。请帮帮我。如果它有任何用处,我必须运行它的服务器是amazon ec2 linux服务器。听起来好像CI没有找到你的MY_Upload类。检查文件名中的大小写字符,当unix系统不区分大小写时,windows不区分大小写。您可以通过在控制器中打印
    $this->upload
    实例的类名来检查它是否工作<代码>打印获取类($this->upload)应该显示
    我的上传
    。再次感谢!我将文件保存为我的上传,而不是我的上传。它现在在两个地方都能正常工作。再次感谢你的帮助。
    $this->upload->do_upload('userfile', true);
    
    $img = imagecreatefromstring(base64_decode($string)); 
    if($img != false) 
    { 
       imagejpeg($img, '/path/to/new/image.jpg'); 
    }