2012-07-20 47 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