【问题标题】:Codeigniter 2.2: File UploadCodeigniter 2.2:文件上传
【发布时间】:2015-08-10 09:04:20
【问题描述】:

我在处理 CodeIgniter 2.2 中的文件上传时遇到问题。 我能够创建其特定的文件夹目的地,但我无法上传我选择的文件。

这是我的控制器

function create_report()
{
    if($this->session->userdata('logged_in'))
    {

            $this->create_model->set_report();


            $this->session->set_flashdata('message', 'Success! You created a Report!');
            #$redirect($_SERVER['HTTP_REFERER'], 'refresh');


     }
   else
   {
            $this->session->set_flashdata('message', 'Oops! You have to Login');
     //If no session, redirect to login page
            redirect('login', 'refresh');
   }      

}

这是我的模型

function set_report()
    {
    if($this->session->userdata('logged_in'))
        {       
                $session_data = $this->session->userdata('logged_in');

                if($_FILES['userfile']['name'] != NULL)
                {       
                        $main_dir = './FILES/'.$this->input->post('patientname').'/';
                        // Check if User Folder is already created. Create New if none exist
                        if(!is_dir($main_dir)){
                        mkdir($main_dir, 0777);
                        }

                        $target_dir = './FILES/'.$this->input->post('patientname').'/'.$this->input->post('session_id').'/';
                        // Check if Session Folder is already created. Create New if none exist
                        if(!is_dir($target_dir)) {
                        mkdir($target_dir, 0777);
                        }

                        $config['upload_path'] = './FILES/'.$this->input->post('patientname').'/'.$this->input->post('session_id').'/';
                        $config['allowed_types'] = 'gif|jpg|png|docx|xlsx|doc|pdf|csv|zip|rar|7zip|ppt|pptx';


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



                        $data2 = array('upload_data' => $this->upload->data());



                }

                $data = array(                      
                    'session_id' => $this->input->post('session_id'),   
                    'report_creator' => $session_data['username'],
                    'report_patientname' => $this->input->post('patientname'),
                    'report_patientid' => $this->input->post('patientid'),
                    'report_subject' => $this->input->post('subject'),                      
                    'report_description' => $this->input->post('description'),
                    'report_time' => $this->input->post('date'),                        
                    'report_date' => $this->input->post('time')                     

                );

                return $this->db->insert('session_reports', $data);

    }


}

我一直在尝试解决这个问题,但我还没有弄清楚关键。 我希望任何人都可以帮助我。

【问题讨论】:

  • 所以?你的错误是什么?您是否尝试显示 $this->upload->display_errors() 以查看问题所在?
  • 顺便说一句,您忘记了$this->upload->do_upload($field_name) 来上传您的文件。阅读文档:ellislab.com/codeigniter/user-guide/libraries/…
  • 你确定mkdir($main_dir, 0777) 工作正常吗??
  • @VincentDecaux 没有错误显示,我也尝试过 ` $field_name = "userfile"; $this->upload->do_upload($field_name); `
  • @Abdulla。是的。它正在工作

标签: php codeigniter


【解决方案1】:

我相信,我未能初始化配置。

$this->upload->initialize($config);     

这解决了我的问题。谢谢大家

【讨论】:

    【解决方案2】:

    删除这个

    $data2 = array('upload_data' => $this->upload->data());
    

    把这个换成那个

    if (!$this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
    
        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
    
        $this->load->view('upload_success', $data);
    }
    

    加载库

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

    您也可以通过调用initialize() 方法来设置首选项。如果您自动加载类,则很有用:

    $this->upload->initialize($config);
    

    Codeigniter file Upload

    【讨论】:

      猜你喜欢
      • 2016-07-25
      • 1970-01-01
      • 2011-10-19
      • 2011-11-06
      • 1970-01-01
      • 2013-09-12
      • 2015-12-26
      相关资源
      最近更新 更多