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

Posted

技术标签:

【中文标题】Codeigniter上传:插入数据库时​​图像的文件名返回null【英文标题】:Codeigniter upload: filename of the image is returning null when inserting in database 【发布时间】:2015-09-10 20:40:42 【问题描述】:

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

控制器:

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

【问题讨论】:

你在表单标签中使用 enctype= multipart/form-data 吗? 是... 'postValidation')); ?> 您没有使用 codeigniter do_upload? @Noobie 我添加了我的答案,也不需要使用 $query 插入, 【参考方案1】:

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

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



【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于Codeigniter上传:插入数据库时​​图像的文件名返回null的主要内容,如果未能解决你的问题,请参考以下文章

带有 Summernote 的 CodeIgniter - 无法将图像上传到数据库

如何使用codeigniter在数据库中插入图像

使用 CodeIgniter 上传多张图片

Codeigniter缩略图图像路径不能存储在MySql中

如何在codeigniter中上传多个/多个图像[重复]

使用ajax在codeigniter中上传图片