Codeigniter上传:在数据库中插入时,图像的文件名返回null


Codeigniter upload: filename of the image is returning null when inserting in database

每次运行站点时,它总是在数据库中返回一个空白数据(file_name)。有人能给出代码中的错误,或者给出一些建议,或者做什么是正确的吗?提前谢谢。

控制器:

class User_controller extends CI_Controller {
  public function postValidation(){
    $this->load->library('form_validation');
    $this->load->model('post_model');
    $this->form_validation->set_rules('postTxtarea', 'Posttxtarea', 'required|xss_clean' );
    $config['upload_path'] = './uploads';
    $config['allowed_types'] = 'gif|jpg|png|docx';
    $config['max_size'] = '1000';
    $this->load->library('upload', $config);
    if(empty($_FILES['fileUpload']['name']) == false){
      //photo
      $this->user_model->postImageModel();
      $this->session->set_flashdata('message', "<p class='bg-success'>File updated successfully.</p>");
      redirect('userhome'); 
    } else if($this->form_validation->run() == true){
      //text
      if($this->user_model->postValidationModel() == true){
        $this->session->set_flashdata('message', "<p class='bg-success'>Post updated successfully.</p>");
        redirect('userhome');   
      }
    } else {
      $this->session->set_flashdata('message', "<p class='bg-danger'>Post update should have something written or a photo or attach a file.</p>");
      redirect('userhome');
    }
  }
}

型号:

class User_model extends CI_Model {
  public function postValidationModel(){
    $today = date("Y-m-d H:i:s");
    $data = array(
        'user_id' => $this->session->userdata['acct_id'],
        'room_id' => $this->input->post('postSelectRoom'),
        'date_created' => $today,
        'message' => $this->input->post('postTxtarea'),
    );
    $query = $this->db->insert('posts_tb', $data);
    if($query){
      return true;
    } else {
      return false;
    }
  }
  public function postImageModel(){
    $image_data = $this->upload->data();
    $data = array(
        'user_id' => $this->session->userdata['acct_id'],
        'room_id' => $this->input->post('postSelectRoom'),
        'caption' => $this->input->post('postTxtarea'),
        'file_path' => "uploads/",
        'file_name' => $image_data['file_name'],            
    );
    $query = $this->db->insert('uploads_tb', $data);
    if($query){
      return true;
    } else {
      return false;
    }
  }
}

数据库:

http://prntscr.com/7ks6jt

我认为上传带有表单验证的文件的更好选择是使用回调函数

http://www.codeigniter.com/userguide2/libraries/file_uploading.html

http://www.codeigniter.com/userguide2/libraries/form_validation.html

http://www.codeigniter.com/docs

控制器

class User extends CI_Controller {
    public function postValidation() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('postTxtarea', 'postTxtarea', 'required|callback_do_upload');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('some_view');
        } else {
            redirect('userhome');
        }
    }
    public function do_upload() {
        // Codeigniter upload for single files only.
        // Uploads folder must be in main directory.
        $config['upload_path'] = './uploads/'; // Added forward slash 
        $config['allowed_types'] = 'gif|jpg|png|docx'; // Not sure docx will work?
        $config['max_size'] = '1000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['overwrite'] = TRUE;
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $input = "userfile"; // on view the name <input type="file" name="userfile" size="20"/>
        if (!$this->upload->do_upload($input)) {
            $this->form_validation->set_message('do_upload', 'Post update should have something written or a photo or attach a file.');
            return false; // Must have false after message for error message to show
        } else {
            $upload_data = $this->upload->data();
            $this->user_model->postImageModel($upload_data = array());
            return true;
        }
    }
}

型号

class Model extends CI_Model {
public function postImageModel($upload_data = array()) {
$data = array(
    'user_id' => $this->session->userdata('acct_id'), // use () and not []
    'room_id' => $this->input->post('postSelectRoom'),
    'caption' => $this->input->post('postTxtarea'),
    'file_path' => $upload_data['upload_path'],
    'file_name' => $upload_data['file_name']            
);
$this->db->insert('uploads_tb', $data);
}
}

@wolfgang1983我试过你的代码,但我无法完成。之后我注意到do_upload验证,并尝试进行处理。谢谢你指出它。

if($this->form_validation->run() == true){
        if ( $this->upload->do_upload()){
        //photo
        $this->user_model->postImageModel();
        $this->session->set_flashdata('message', "<p class='bg-success'>File updated successfully.</p>");
        redirect('userhome');       
    } else
        //text
        if($this->user_model->postAnnouncementModel() == true){
            $this->session->set_flashdata('message', "<p class='bg-success'>Announcement updated successfully.</p>");
            redirect('userhome');   
        }
    } else {
        $this->session->set_flashdata('message', "<p class='bg-danger'>Announcement update should have something written or photo or attach a file.</p>");
        redirect('userhome');
    }