无法使用CI控制器将文件上传到codeigniter文件夹

时间:2018-07-30 08:56:21

标签: php html codeigniter codeigniter-3

我正在制作一个表单,该人员可以在其中添加图片,该图片将显示在本地目录文件夹中,并显示在sql表中。但是看来我尝试上传的图片多少次都无法上传!请帮忙!

控制器:

    <?php

if (!defined('BASEPATH'))
   exit('No direct script access allowed');

class Auth extends MY_Controller {

   function __construct() {
       parent::__construct();
       $this->load->database();
       $this->load->library('session');
       $this->load->library(array('ion_auth', 'form_validation'));
       $this->load->helper(array('url', 'language', 'form'));
$this->load->model('Ion_auth_model');

       $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));

       log_message('debug', 'CI My Admin : Auth class loaded');
   }

   public function index() {
       $data['page'] = $this->config->item('englishlivebali_template_dir_public') . "login_form";
       $data['module'] = 'auth';
       $this->load->view($this->_container, $data);
   }

   public function login_form(){
       if ($this->ion_auth->logged_in()) {
           redirect('student', 'refresh');
       } else {
           $data['page'] = $this->config->item('englishlivebali_template_dir_public') . "login_form";
           $data['module'] = 'auth';

           $this->load->view($this->_container, $data);
       }
   }

    public function login_formteacher(){
       if ($this->ion_auth->logged_in()) {
           redirect('teacher', 'refresh');
       } else {
           $data['page'] = $this->config->item('englishlivebali_template_dir_public') . "login_formteacher";
           $data['module'] = 'auth';

           $this->load->view($this->_container, $data);
       }
   }

   public function login() {
       $this->form_validation->set_rules('username', 'Username', 'required');
       $this->form_validation->set_rules('password', 'Password', 'required');

       if ($this->form_validation->run() == true) {
           $remember = (bool) $this->input->post('remember');

           if ($this->ion_auth->login($this->input->post('username'), $this->input->post('password'), $remember)) {
               if ($this->input->post('username')=='teacher') {
                   $this->session->set_flashdata('message', $this->ion_auth->messages());
                   redirect('/teacher/dashboard', 'refresh');
               }
               elseif ($this->input->post('username')!=='teacher') {
                    $this->session->set_flashdata('message', $this->ion_auth->messages());
                   redirect('/student/index', 'refresh');
               } 
               else {
                   $this->session->set_flashdata('message', $this->ion_auth->messages());
                   redirect('/student/dashboard', 'refresh');
               }
           } else {
               $this->session->set_flashdata('message', $this->ion_auth->errors());
               redirect('auth/login_form', 'refresh');
           }
       }
   }

    public function logout() {
        $this->ion_auth->logout();

        redirect('auth', 'refresh');
    }


/* End of file auth.php */
/* Location: ./modules/auth/controllers/auth.php */


    public function users_save()
{
    $username = $_POST["username"];
    $email = $_POST["email"];
    $salt       = $this->Ion_auth_model->store_salt ? $this->Ion_auth_model->salt() : FALSE;
    $password = $this->Ion_auth_model->hash_password($_POST["password"], $salt);

    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');
    if($this->form_validation->run()===FALSE)
    {
        $this->session->set_flashdata('gagal_user_save', 'Gagal Menambahkan User Baru');
            redirect('auth');
    }
    else {
    $this->db->query("INSERT INTO users (username, email, password, active) values ('$username', '$email', '$password', 1)");
    $idn = $this->db->query("select id from users order by id desc limit 1")->result();
    foreach ($idn as $val => $value ){
        $idValue = $value->id; 
    }

    $this->db->query("insert into users_groups (user_id, group_id) values ('$idValue','$idgz')");
    $this->session->set_flashdata('sukses_user_save', 'Sign Up Success, Now Please Log In');
    redirect ('auth');
    }
}

    public function biodata()
{
    $namad = $_POST["namad"];
    $namab = $_POST["namab"];
    $tempat_lahir = $_POST["tempat_lahir"];
    $tgl_lahir = $_POST["tgl_lahir"];
    $jk = $_POST["jk"];
    $agama = $_POST["agama"];
    $ayah = $_POST["ayah"];
    $ibu = $_POST["ibu"];
    $alamat = $_POST["alamat"];

    $idz = $this->input->post('up');
    $this->db->query("UPDATE users SET first_name = '$namad', last_name = '$namab', tempat_lahir = '$tempat_lahir', tgl_lahir = '$tgl_lahir', jk = '$jk', agama = '$agama', ayah = '$ayah', ibu = '$ibu', alamat = '$alamat' WHERE id = '$idz'");
    redirect ('student/biodata');
}

public function do_upload()
{


                $config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png|jpeg';

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

                if ( ! $this->upload->do_upload()) 
                {
                        $error = array('error' => $this->upload->display_errors());
                        redirect ('student/biodata');

                }else{
                        $file_data = $this->upload->data();
                        $data['img'] = base_url().'/uploads/'.$file_data['file_name'];

                    }
                }

            }

表单中的输入类型的ID为“ gambar_produk”,并且名称为“ gambar_produk”。我尝试使用的函数是do_upload函数。我尝试搜索各种方法,但仍然无法正常工作。请帮忙!

2 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

注意:您的表单应具有属性enctype="multipart/form-data", 更好地使用form_open_multipart();

带有$this->upload->do_upload()的种族$this->upload->do_upload('gambar_produk'),其中gambar_produk是文件输入的名称

您的方法do_upload应该是这样的:

public function do_upload()
{
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('gambar_produk')) 
    {
      $error = array('error' => $this->upload->display_errors());
      redirect ('student/biodata');

    }else
    {
      $file_data = $this->upload->data();
      $data['img'] = base_url().'/uploads/'.$file_data['file_name'];

    }
}

更多信息:https://www.codeigniter.com/userguide3/libraries/file_uploading.html

答案 1 :(得分:0)

默认情况下, do_upload 希望文件来自名为 userfile 的表单字段,并且表单的类型必须为“ multipart”。

要使其生效,请将输入文件 gambar_produk 的名称更改为用户文件或使用

if ( ! $this->upload->do_upload('gambar_produk')) 
{
    $error = array('error' => $this->upload->display_errors());
    redirect ('student/biodata');

}else{
    $file_data = $this->upload->data();
    $data['img'] = base_url().'/uploads/'.$file_data['file_name'];

}