Как вставить имя файла в базу данных и файл в папке с помощью codeignator? - PullRequest
0 голосов
/ 11 октября 2018

У меня проблема с вставкой file_path в базу данных и загрузкой файла в папку с использованием Codeigniter, у меня есть поле формы, где пользователь может загрузить любой файл 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 голосов
/ 11 октября 2018
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';
 }  
0 голосов
/ 11 октября 2018
$config = array(
                'upload_path'   =>'./uploads/',
                'allowed_types' =>'jpg|jpeg|png|gif',
                'max_size'      => '5000KB');

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

Замените этот код своим кодом .... и проверяйте ......

0 голосов
/ 11 октября 2018
 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...