2011-09-22 76 views
2

我正在做一個腳本,將上傳圖像並保存圖像路徑在數據庫中。我唯一的問題是如果用戶沒有上傳圖像,我想設置默認圖像。設置默認圖像,如果沒有圖像被選中上載在codeigniter

但在codeigniter中,當圖像未被選中時,它會自動給出一個錯誤,說明沒有選擇輸入文件。

我控制器

if (!$this->upload->do_upload('image')) 
    { 
    $error = array('error' => $this->upload->display_errors()); 
    $this->load->view('upload_success', $error); 

    } 
    else { 
     $image_data=array('image_info' => $this->upload->data('image')); 
     $image_path=$image_data['image_info']['full_path']; 

     $data =array(
     'submitedby'=>$username, 
     'city'=>$this->input->post('towncity'), 
     'image' => $image_path 

); 
    } 

可有人請建議我如何,如果用戶沒有顯示默認的錯誤選擇的圖像設置一個默認的形象?

回答

3

在​​3210失敗的條款中,檢查文件是否已上傳。

if (!$this->upload->do_upload('image')) { 

    if (!empty($_FILES['image']['name'])) { 
     // Name isn't empty so a file must have been selected 
     $error = array('error' => $this->upload->display_errors()); 
     $this->load->view('upload_success', $error); 
    } else { 
     // No file selected - set default image 
     $data = array(
      'submitedby' => $username, 
      'city'  => $this->input->post('towncity'), 
      'image'  => 'path/to/default/image', 
     ); 
    } 

} else { 
    $image_data = array('image_info' => $this->upload->data('image')); 
    $image_path = $image_data['image_info']['full_path']; 

    $data = array(
     'submitedby' => $username, 
     'city'  => $this->input->post('towncity'), 
     'image'  => $image_path, 
    ); 
} 

這可以進一步重構,但問題是,你可以檢查$_FILES['field_name']['name'],看看是否被選中的文件。

1

如果沒有找到圖像,您可能無法制作所需的圖像字段,然後可以在視圖中設置默認圖像。這樣,您就不需要上傳各種重複圖片,只要您想要,您就可以輕鬆地爲每個人更新默認圖片。

相關問題