2012-07-20 53 views
2

我必须上传我从android应用程序接收的base64编码图像。我使用php codeigniter框架。 在搜索论坛时,此链接How to upload base64encoded image in codeigniter的问题与我的相同,但解决方案不适合我。无法使用Codeigniter上传base64编码图像

这里是我写的代码:

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

我收到“您没有选择要上传的文件”

感谢您的时间。

回答

3

发生此错误的原因在于codeigniter的上传库将查看$_FILES超全球范围并搜索您在​​3210调用时提供的索引。

此外(至少在版本2.1.2中),即使您将设置$ _FILES超全球模仿文件上传的行为,它也不会通过,因为上传库使用is_uploaded_file来检测那种篡改与超球。你可以跟踪系统/库/ Upload.php中的代码:恐怕你将不得不重新实现大小检查和文件重命名和移动(我会这样做),或者你可以修改代码为省略该检查,但可能会使框架升级变得困难。

  1. 的$图像变量的内容保存到一个临时文件,并成立了$_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'], 
    ); 
    
  2. 修改上传库。您可以使用CodeIgniter的内置way of Extending Native Libraries,并定义My_Upload(或前缀)类,复制 - 粘贴do_upload功能和更改以下行:

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

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

嗨,谢谢你的回复。我按照您的指导进行了更改,但出现以下错误: 1。'$ this-> file_name = $ this - > _ prep_filename($ _ FILES [$ field] ['name'])的行号86处的未定义索引; '在我在MY_Upload类中复制的do_upload函数中。 2.您正试图上传的文件类型是不允许的。这是print_r'Array([userfile] => Array([tmp_name] => C:\ Windows \ Temp \ and15BC.tmp [size] => 354129 [error] => 0 [type] => image/png))' – Parth 2012-07-20 13:53:28

+0

我忘记了'$ _FILES'中的'name'键,更新了这个例子,它应该修复这两个错误。 – complex857 2012-07-20 14:07:47

+0

嗨非常感谢你。它的工作现在 – Parth 2012-07-20 19:29:17

1

大家都知道,如果您收到了Base64编码的图像,作为一个字符串,那么你不需要使用Upload类。

相反,你只需要使用BASE64_DECODE将其解码,然后用FWRITE/file_put_contents保存解码后的数据...

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

来源:http://board.phpbuilder.com/showthread.php?10359450-RESOLVED-Saving-Base64-image