Codeigniter没有上传图片

时间:2016-07-21 09:05:05

标签: php codeigniter

问题

我有注册学生的表格。现在我试图插入数据与上传图像但数据库只有文件名。上传文件是空的!我的观看表单输入类型为' file'。

控制器

$config['upload_path'] = APPPATH.'uploads';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $this->upload->do_upload('image');
    $this->load->library('upload',$config);

    if($this->form_validation->run())
    {
        $data = array(
            'student_name'=>$this->input->post('name'),
            'student_surname'=>$this->input->post('surname'),
            'student_middlename'=>$this->input->post('middlename'),
            'student_birth'=>$this->input->post('date'),
            'student_add_date' => date('Y-m-d'),
            'student_gender'=>$this->input->post('gender'),
            'student_addres'=>$this->input->post('address'),
            'student_mobile'=>$this->input->post('phone'),
            'student_email'=>$this->input->post('email'),
            'student_image'=>$this->upload->do_upload('image')

        );

        $this->member->add_student($data);
        redirect('members/student_list');               
    }

4 个答案:

答案 0 :(得分:0)

你可以这样做,

$config['upload_path'] = 'uploads/';

答案 1 :(得分:0)

请提供以下声明,而不是$ config ['upload_path'] = APPPATH .'uploads';

$config['upload_path'] = './uploads/';

并确保在应用程序文件夹外部创建上传文件夹

答案 2 :(得分:0)

试试这段代码:

if($this->form_validation->run())
{
      //configure and load upload library
        $config['upload_path'] =getcwd().'/uploads';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '10000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

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

   //if can't upload image exit the function 
    if (!$this->upload->do_upload('image')) {
        $error = $this->upload->display_errors();
        //use var_dump only developing environment
        var_dump($this->upload->display_errors());
       // exit();
    }

    $data = array(
        'student_name'=>$this->input->post('name'),
        'student_surname'=>$this->input->post('surname'),
        'student_middlename'=>$this->input->post('middlename'),
        'student_birth'=>$this->input->post('date'),
        'student_add_date' => date('Y-m-d'),
        'student_gender'=>$this->input->post('gender'),
        'student_addres'=>$this->input->post('address'),
        'student_mobile'=>$this->input->post('phone'),
        'student_email'=>$this->input->post('email'),
        // 'student_image'=>$this->upload->do_upload('image'),

    );



    $this->member->add_student($data);
    redirect('members/student_list');               
}

答案 3 :(得分:0)

将.htaccess文件更改/编辑为:

              <IfModule mod_rewrite.c>
                 RewriteEngine On
                 RewriteCond %{REQUEST_FILENAME} !-d
                  RewriteCond %{REQUEST_FILENAME} !-f
                  RewriteRule ^(.*)$ index.php/$1 [QSA,L]
              </IfModule> 

然后在你的控制器集中:

                  $upload_dir = 'upload_folder';
                  $config['upload_path'] =  $upload_dir ;
                  $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '10000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

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