2017-08-11 68 views
0

我有這個代碼的小問題在我的系統上添加一個產品我使用V_add_pro函數,當圖像上傳部分來了我面臨一些問題來驗證,所以我使用了一個不同的函數來通過回調進行驗證,它不工作。主要問題是我只將圖像名稱保存到數據庫,那麼如何僅返回圖像名稱?如果你們在我的代碼中發現任何問題,請告訴我。謝謝。Codeignitor圖像上傳驗證與控制器和功能

public function v_add_pro(){ 
    $this->load->model('B_menu'); 
    $data = array('status' => false, 'msg' => array()); 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules("pro_name", "name needed", "trim|required"); 
    $this->form_validation->set_rules("pro_price", "price needed", "trim|required"); 
    $this->form_validation->set_rules("pro_desc", "description is needed", "trim|required"); 
    $this->form_validation->set_rules("dropzone", "select an image", "trim|required|callback_validate_image"); 
    $this->form_validation->set_rules("pro_cat", "please select a category","callback_check_default|trim|required"); 
    $this->form_validation->set_error_delimiters('<p class="text-danger">','</p>'); 
    if ($this->form_validation->run()) { 
     $data['status'] = true; 

     $pro_data = array(
      'pname' => $this->input->post('pro_name'), 
      'pprice' => $this->input->post('pro_price'), 
      'pdesc' => $this->input->post('pro_desc'), 
      'pimage' =>$this->input->post('dropzone'), 
      'catid' => $this->input->post('pro_cat'), 
     ); 

     $this->B_menu->add($pro_data); 

    } else { 
     foreach ($_POST as $key => $value) { 
      $data['msg'][$key] = form_error($key); 
     } 
    } 

    echo json_encode($data); 

} 

//檢查所選值類別

function check_default($post_string) 
{ 
    return $post_string == '0' ? FALSE : TRUE; 
} 

//將圖片上傳到正確的路徑

public function validate_image() { 
    $config = array(
     'allowed_types'=>'jpg|png|gif', 
     'upload_path'=> realpath(APPPATH . '../skin/images'), 
     'max_size'=>1 
    ); 
    $this->load->library('upload', $config); 
    if (!$this->upload->do_upload('dropzone')) 
    { 
     $this->form_validation->set_message('validate_image',$this->upload->display_errors()); 
    } else { 
     $file_info = $this->upload->data(); 
     $img = $file_info['file_name']; 
     return $img; 
    } 
} 
+0

你可以請拋出錯誤,這樣我們就可以直接解決 –

回答

0
public function validate_image() 
{ 
/* Start: Image Uploading */ 
      $baseurl = $this->config->base_url(); 
      $profile_photo = ""; 
      $profile_path = ''; 
      $config['upload_path'] = './assets/profile_pictures'; 
      $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
      $config['max_size'] = '0'; 
      $config['max_width'] = '0'; 
      $config['max_height'] = '0'; 
      if (!empty($_FILES)) 
      { 
       $this->file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); 
       $this->file_name = time() . "." . $this->file_ext; 
       $config['file_name'] = $this->file_name; 
       $this->load->library('upload', $config); 
       $this->upload->initialize($config); 

       if (!$this->upload->do_upload('image', FALSE)) 
       { 
        echo $this->upload->display_errors(); 
        $this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors()); 
        if ($_FILES['image']['error'] != 4) 
        { 
         return false; 
        } 
       } 
       else 
       { 
        $profile_photo = $this->file_name; 
        $profile_path = $baseurl . "assets/profile_pictures"; 
        return $profile_photo; 
       } 
      } 

     /* End: Image Uploading */ 

要附加我只是將文件名保存爲當前時間戳即可。也返回$ imagename也嘗試呼應這個

echo $this->upload->display_errors(); 
+0

感謝隊友,其實我得到一個錯誤,一旦我呼應顯示錯誤 – Sazny14

+0

消息:的對象類CI_Form_validation無法轉換爲字符串 – Sazny14

+0

我給出的代碼嘗試它是否工作? –