簡體   English   中英

Codeigniter沒有上傳圖片

[英]Codeigniter not uploading image

問題

我有注冊學生的表格。 現在,我試圖插入帶有上傳圖像的數據,但是到數據庫中只有文件名。 上傳文件為空! 我的視圖表單輸入類型為“文件”。

控制者

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

你可以做到這一點,

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

請提供以下語句,而不是$ config ['upload_path'] = APPPATH。

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

並確保您在應用程序文件夾外部創建的上載文件夾

試試這個代碼:

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

將您的.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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM