Php Codeigniter上载:插入数据库时,图像的文件名返回null

Php Codeigniter上载:插入数据库时,图像的文件名返回null,php,database,codeigniter,phpmyadmin,Php,Database,Codeigniter,Phpmyadmin,每次我运行站点时,它总是在数据库中返回一个空白数据(文件名)。任何人都可以给出代码中的错误,或者给出一些建议,或者什么是正确的做法吗?提前谢谢 控制器: class User_controller extends CI_Controller { public function postValidation(){ $this->load->library('form_validation'); $this->load->model('post_mode

每次我运行站点时,它总是在数据库中返回一个空白数据(文件名)。任何人都可以给出代码中的错误,或者给出一些建议,或者什么是正确的做法吗?提前谢谢

控制器:

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');
    }
  }
}
DB:


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

控制器

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;
        }
    }
}

@沃尔夫:我试过你的代码,但我无法通过。在那之后,我注意到do_上传验证,并尝试进行处理。谢谢你指出这一点

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');
    }
if($this->form\u validation->run()==true){
如果($this->upload->do_upload()){
//照片
$this->user_model->postmagemodel();
$this->session->set_flashdata('message',“

文件更新成功。

”; 重定向('userhome'); }否则 //正文 如果($this->user\u model->PostAnnounceModel()==true){ $this->session->set_flashdata('message',“

公告已成功更新。

”; 重定向('userhome'); } }否则{ $this->session->set_flashdata('message',“

公告更新应该有一些文字、照片或附加文件。

”; 重定向('userhome'); }
您是否在表单标签中使用enctype=multipart/form data?是的……您没有使用codeigniter进行上传吗?@Noobie我添加了我的答案,也不需要使用$query和insert,
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);
}

}
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');
    }