2016-01-09 37 views
0

我有一個接受一些文本輸入字段和一個文件字段(用於上傳圖片)的表單。CodeIgniter - 使用圖片上傳的表單驗證

我的問題是這樣的,如果我沒有填寫必填字段之一併且我選擇了有效的圖片文件,我會收到有關缺少字段的錯誤消息,但圖片將被上傳。控制器是:

defined('BASEPATH') OR exit('No direct script access allowed'); 

    class Manage extends MY_Controller 
    { 
     public function __construct() 
     { 
      parent::__construct(); 
     } 

     public function index() 
     { 
      $config = array(
       array(
        'field' => 'item_name', 
        'label' => 'Item name', 
        'rules' => 'required' 
       ), 
       array(
        'field' => 'item_code', 
        'label' => 'Item code', 
        'rules' => 'required|is_unique[_items.item_code]' 
       ), 
       array(
        'field' => 'item_description', 
        'label' => 'Item description', 
        'rules' => 'required' 
       ), 
       array(
        'field' => 'item_img', 
        'label' => 'Item image', 
        'rules' => 'callback_upload_image' 
       ) 
      ); 

      $this->form_validation->set_rules($config); 
      $this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.'); 

      if($this->form_validation->run() == FALSE) 
      { 
       // Render the entry form again 
      } 
      else 
      { 
       // Go to another page 
      } 
     } 

     public function upload_image() 
     { 
      $config['upload_path'] = './items_images'; 
      $config['max_size'] = 1024 * 10; 
      $config['allowed_types'] = 'gif|png|jpg|jpeg'; 
      $config['encrypt_name'] = TRUE; 

      $this->load->library('upload', $config); 

      if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name'])) 
      { 
       if($this->upload->do_upload('item_img')) 
       { 
        $upload_data = $this->upload->data(); 
        $_POST['item_img'] = $upload_data['file_name']; 
        return TRUE; 
       } 
       else 
       { 
        $this->form_validation->set_message('upload_image', $this->upload->display_errors()); 
        return FALSE; 
       } 
      } 
      else 
      { 
       $_POST['item_img'] = NULL; 
       return FALSE; 
      } 
     } 
    } 

據我所知,如果一個規則失敗,其他領域和操作將被取消,該表格將被再次加載,或其他操作就搞定。

謝謝,,,

+0

ok現在你的問題是什麼?你想要什麼?出現什麼錯誤 –

+0

如果我沒有填寫其中一個必填字段,並且我選擇了有效的圖片文件,我會收到有關缺少字段的錯誤消息,但該圖片仍會上傳。 –

+0

如果按需填寫的字段表示帶有HTML 5必填屬性的表單字段,則表單不會提交。所以圖片不會上傳。 – ThatsME

回答

0

您只有在驗證爲真時才需要上傳圖片。所以刪除你的公共函數upload_image()並在驗證true語句內寫下如下所示的功能。

defined('BASEPATH') OR exit('No direct script access allowed'); 
class Manage extends MY_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 
    public function index() 
    { 
     $config = array(
      array(
       'field' => 'item_name', 
       'label' => 'Item name', 
       'rules' => 'required' 
      ), 
      array(
       'field' => 'item_code', 
       'label' => 'Item code', 
       'rules' => 'required|is_unique[_items.item_code]' 
      ), 
      array(
       'field' => 'item_description', 
       'label' => 'Item description', 
       'rules' => 'required' 
      ), 
      array(
       'field' => 'item_img', 
       'label' => 'Item image', 
       'rules' => 'callback_upload_image' 
      ) 
     ); 

     $this->form_validation->set_rules($config); 
     $this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.'); 

     if($this->form_validation->run() == FALSE) 
     { 
      // Render the entry form again 
     } 
     else 
     { 
      $config['upload_path'] = './items_images'; 
     $config['max_size'] = 1024 * 10; 
     $config['allowed_types'] = 'gif|png|jpg|jpeg'; 
     $config['encrypt_name'] = TRUE; 

     $this->load->library('upload', $config); 

     if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name'])) 
     { 
      if($this->upload->do_upload('item_img')) 
      { 
       $upload_data = $this->upload->data(); 
       $_POST['item_img'] = $upload_data['file_name']; 
       return TRUE; 
      } 
      else 
      { 
       $this->form_validation->set_message('upload_image', $this->upload->display_errors()); 
       return FALSE; 
      } 
     } 
     else 
     { 
      $_POST['item_img'] = NULL; 
      return FALSE; 
     } 
    } 
    // Go to another page 
     } 
    } 
0

我發現這個非常有幫助後here。作者寫了自己的規則來驗證用戶選擇的文件上傳。代碼示例:

$this->form_validation->set_rules('file', '', 'callback_file_check'); 


public function file_check($str){ 
    $allowed_mime_type_arr = array('application/pdf','image/gif','image/jpeg','image/pjpeg','image/png','image/x-png'); 
    $mime = get_mime_by_extension($_FILES['file']['name']); 
    if(isset($_FILES['file']['name']) && $_FILES['file']['name']!=""){ 
     if(in_array($mime, $allowed_mime_type_arr)){ 
      return true; 
     }else{ 
      $this->form_validation->set_message('file_check', 'Please select only pdf/gif/jpg/png file.'); 
      return false; 
     } 
    }else{ 
     $this->form_validation->set_message('file_check', 'Please choose a file to upload.'); 
     return false; 
    } 
}