2013-05-14 71 views
0

我需要一些幫助來找出編輯頁面。Codeigniter:如何在沒有編輯圖像輸入的情況下編輯表單

我有插入數據和編輯數據。

在編輯數據也請求文件/圖像上傳的表單,並在提交時發送null到列文件/圖像。

我需要的是,如果沒有新文件在文件/圖像輸入上跳過此輸入,編輯。

這是我的控制器來插入和編輯數據。

// Set up the form 
     $rules = $this->image_m->rules; 
     $this->form_validation->set_rules($rules); 

     // Upload File Image 
     $status = ""; 
     $msg = ""; 
     $file_element_name = 'file_name'; 

     if ($status != "error") 
     { 
      $config['upload_path'] = './uploads/images/'; 
      $config['allowed_types'] = 'gif|jpg|png|doc|txt'; 
      $config['max_size'] = '2000'; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '768'; 
      $config['encrypt_name'] = FALSE; 
      $config['remove_spaces'] = TRUE; 

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

      if (!$this->upload->do_upload($file_element_name)) 
      { 
      $status = 'error'; 
      $msg = $this->upload->display_errors('', ''); 
      } 
      else 
      { 
      $image = $this->upload->data(); 
      } 
      } 



     // Process the form 
     if ($this->form_validation->run() == TRUE) { 
      $data = array(
       'pubdate' => $this->input->post('pubdate'), 
       'enddate' => $this->input->post('enddate'), 
       'name' => $this->input->post('name'), 
       'image'  => $image['file_name'] 
      ); 
      $this->image_m->save($data, $id); 
      redirect('admin/image'); 
     } 

任何幫助表示讚賞。

解決方案:

// Process the form 
     if ($this->form_validation->run() == TRUE) { 
      if(is_null($image['file_name'])){ 
      $data = array(
       'pubdate' => $this->input->post('pubdate'), 
       'enddate' => $this->input->post('enddate'), 
       'name' => $this->input->post('name'), 
       'caption' => $this->input->post('caption') 
      ); 
      } else { 
      $data = array(
       'pubdate' => $this->input->post('pubdate'), 
       'enddate' => $this->input->post('enddate'), 
       'name' => $this->input->post('name'), 
       'image'  => $image['file_name'], 
       'caption' => $this->input->post('caption') 
      ); 
      } 

回答

0
$img = null; 

//Check if a file is being uploaded/updated 
if($_FILES['fieldname'][tmp_name] != ''){ 
    //upload file here..... 

    $file = $this->upload->data(); 
    $img = $file['file_name']; 
} 

//if no file name assigned to var, use the data stored in the model 
//default_image.jpg 
if(is_null($img)) $img = (string)$model->image; 

-

`image` varchar(255) not null default 'default_image.jpg' 
+0

你好Philip,對不起,但我不明白如何重建表單驗證和do_upload。你能否根據我的例子提示評論?多謝。 – Dario

+0

我使用你的想法解決了這個問題。我用解決方案更新了這個問題。 – Dario

0

如果($ _ FILES [ '形象'] [不對tmp_name] = ''!)將正常工作......

相關問題