2012-09-10 47 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); 
} 

希望这有助于!