如何使用Codeignator在数据库中插入文件名并在文件夹中插入文件?

时间:2018-10-11 07:49:22

标签: php html5

我在使用Codeigniter将file_path插入数据库和文件上传到文件夹中时遇到问题,我有一个表单字段,用户可以在其中上传任何jpg | png | gif文件。但是,我将如何上载该特定文件的file_path并将其插入数据库,并将文件存储在floder中。 我的控制器代码

public function enqry(){
            $data = array();
            $postData = array();
            $config['upload_path']   = './uploads/'; 
            $config['allowed_types'] = 'gif|jpg|png'; 
            $config['max_size']      = 10240;
          $this->load->library('upload', $config);
            //if add request is submitted
            if ( ! $this->upload->do_upload('upload')) {
                 $error = array('error' => $this->upload->display_errors()); 
                    $this->load->view('imageUploadForm', $error); 
                     }else { 
                      print_r('Image Uploaded Successfully.');

                  } 
            if($this->input->post('postSubmit')){

                //form field validation rules
                $this->form_validation->set_rules('name', 'post name', 'required');
                $this->form_validation->set_rules('email', 'post email', 'required');
                $this->form_validation->set_rules('mobile', 'post number', 'required');
                $this->form_validation->set_rules('nationality', 'post nationality', 'required');
                $this->form_validation->set_rules('location','post location','required');

                //prepare post data
                $postData = array(
                    'name' => $this->input->post('name'),
                    'email' => $this->input->post('email'),
                    'mobile' => $this->input->post('mobile'),
                    'nationality' => $this->input->post('nationality'),
                    'location'=>$this->input->post('location'),
                    'statuse' => '0',
                    'created_at' => date("Y-m-d H:i:s")
                    /*'passport_details'=>$this->input->post('passport_details')*/
                );

                //validate submitted form data
                if($this->form_validation->run() == true){
                    //insert post data
                    $insert2 = $this->user_mod->insert_enq($postData);
                    if($insert2){
                        $this->session->set_userdata('success_msg', 'Post has been added successfully.');

                        redirect('/User_con/log/');
                    }else{
                        $data['error_msg'] = 'Some problems occurred, please try again.';
                    }
                }
            }

            $data['post'] = $postData;
            $data['title'] = 'Create Post';
          }   
            //load the add page view
              public function log_enq(){          
            $this->load->view('templates/header');
            $this->load->view('enquiry_reg');
            $this->load->view('templates/footer');
        }

我的查看代码

<div class="form-group">
 <label for="title">File Upload</label>
  <input type="file" name="upload" placeholder="upload">
</div>

我的模型代码

public function insert_enq($data = array()) {
        $insert2 = $this->db->insert('tbl_enquiry', $data);
        if($insert2){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

3 个答案:

答案 0 :(得分:0)

 if (!$this->upload->do_upload('upload')) {
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('imageUploadForm', $error); 
 }
 else {  
      $data = $this->upload->data('file_name');
      $image = 'your_path'.$data;
 } 

并将$ image变量添加到您的查询中,例如

$arrayName = array(
    'imapge' => $image,
    'more_column' => $more_column_values);

答案 1 :(得分:0)

public function enqry(){
        $data = array();
        $postData = array();
        $postData['upload_path']   = './uploads/'; 
        $postData['allowed_types'] = 'gif|jpg|png'; 
        $postData['max_size']      = 10240;
      $this->load->library('upload', $postData);
if (!$this->upload->do_upload('upload')) {
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('user_con/error', $error); 
 }
 else {  
      $data = $this->upload->data('file_name');
      $image = 'your_path'.$data;
 } 
        if($this->input->post('postSubmit')){
$this->form_validation->set_rules('name', 'post name', 'required');
            $this->form_validation->set_rules('email', 'post email', 'required');
            $this->form_validation->set_rules('mobile', 'post number', 'required');
            $this->form_validation->set_rules('nationality', 'post nationality', 'required');
            $this->form_validation->set_rules('location','post location','required');
$postData = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'mobile' => $this->input->post('mobile'),
                'nationality' => $this->input->post('nationality'),
                'location'=>$this->input->post('location'),
                'statuse' => '0',
                'created_at' => date("Y-m-d H:i:s"),
                'upload' => $image
 );
if($this->form_validation->run() == true){
$insert2 = $this->user_mod->insert_enq($postData);
if($insert2){
                    $this->session->set_userdata('success_msg', 'Post has been added successfully.');

                    redirect('/User_con/log/');
                }else{
                    $data['error_msg'] = 'Some problems occurred, please try again.';
                }
            }
        }

        $data['post'] = $postData;
        $data['title'] = 'Create Post';
 }  

答案 2 :(得分:0)

$config = array(
                'upload_path'   =>'./uploads/',
                'allowed_types' =>'jpg|jpeg|png|gif',
                'max_size'      => '5000KB');

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

将此代码替换为您的代码...并保持清脆……