2012-09-10 85 views
1

我有一個表單正在提交,用戶有能力上傳該表單中的圖片,然後將整個表單提交。我在codeigniter網站上找到了一個顯示上傳表單的教程(專用於上傳,而不是其他數據)。鏈接是:Codeigniter Upload Tutorial。我怎樣才能提交表單,然後上傳文件,同時也上傳其他細節到數據庫中的其他表?codeigniter與其他數據上傳圖像

回答

0

就可以在其他數據也隨着文件格式的表單控制器通過,像

<?php echo form_open_multipart('upload/do_upload');?> 
    <input type="text" name="someField" value="" /> 
    <input type="file" name="userfile" size="20" /> 
    <input type="submit" value="upload" /> 
</form> 

,並在您上傳控制器的do_upload功能:

$someField = $this->input->post("somefield"); //save to some db 
//and rest of your file to be uploaded code from the same link you provided 

你有沒有意思是這樣的

+0

這項工作?因爲在上傳文件時有一個完整的例子,我可以檢查圖像類型和所有文件。那麼我應該通過$ somefield這種方法,就是這樣嗎? –

+0

是的,當然這會工作,試試看.. :) –

1

下面是一個例子

function add_staff(){ 
      $this->load->library('form_validation'); 
      $this->load->helper(array('form', 'url')); 
      // field name, error message, validation rules 
      $this->form_validation->set_rules('name', 'Full name', 'trim|required'); 
          $this->form_validation->set_rules('designation', 'Last Name', 'trim|required'); 
      if($this->form_validation->run() == FALSE) 
      { 
       $data['main_content'] = 'staff/add_staff'; 
       $this->load->view('admin/template',$data); 
      } 
      else 
      { 
       $config['upload_path'] = 'uploads/staff/'; 
       $config['allowed_types'] = 'gif|jpg|png|jpeg'; 


       $this->load->library('upload', $config); 
       $this->upload->initialize($config); 
       if (! $this->upload->do_upload('file')) 
       { 
              $data['error'] = array('error' => $this->upload->display_errors()); 

        $new_staff = array(
          'name' => $this->input->post('name'), 
          'designation' => $this->input->post('designation'), 
                  'phone' => $this->input->post('phone'), 
                  'email' => $this->input->post('email'), 
                  'address' => $this->input->post('address'), 
                  'description' => $this->input->post('description'), 
          'status' => $this->input->post('status') 
        ); 

       } 
       else 
       { 
        $file_data = $this->upload->data('file'); 

        $new_staff = array(
          'name' => $this->input->post('name'), 
          'designation' => $this->input->post('designation'), 
                  'phone' => $this->input->post('phone'), 
                  'email' => $this->input->post('email'), 
                  'address' => $this->input->post('address'), 
                  'photo' => $file_data['file_name'], 
                  'description' => $this->input->post('description'), 
          'status' => $this->input->post('status') 
        ); 

       }     
        if($this->staff_model->add_staff($new_staff)): 
         $this->session->set_flashdata('success', 'Staff added Sucessfully !!'); 
        endif; 
        redirect('admin/staff');       
      } 

    } 
+0

對不起,但這段代碼的目的是什麼? (2)如果isset($ _POST)並且符合驗證,上傳文件和調用模型,則返回 –

+0

(1)如果不是isset,則重定向到表格($ _ POST) 插入數據庫。 適用於添加和編輯圖像的圖像。如果在編輯時提供圖像,則替換圖像。如果不使用當前圖像 – Sujeet

0

圖片MOO是可以即時完成圖像操作一優庫....

例如,對於上傳的圖片大小調整到一個特定的路徑,你就必須做到這一點

public function thumbnailer($uploader_response,$field_info,$files_to_upload) 
{ 
    $this->load->library('image_moo'); 
$file_uploaded=$field_info->upload_path.'/'.$uploader_response[0]->name; 
$thumbnails=$field_info->upload_path.'/thumbnails/'.$uploader_response[0]->name; 
    $cart_thumbnails=$field_info->upload_path.'/cart_thumbnails/'.$uploader_response[0]->name; 
$this->image_moo->load($file_uploaded)->resize(400,250)->save($thumbnails,false); 
} 

希望這有助於!