【问题标题】:Can't upload file to codeigniter folder using CI Controller无法使用 CI 控制器将文件上传到 codeigniter 文件夹
【发布时间】:2018-07-30 08:56:20
【问题描述】:

我正在制作一个表单,该人可以在其中添加图片,它将显示在本地目录文件夹中并显示在 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 函数。我尝试了各种方法,但它仍然无法正常工作。请帮忙!

【问题讨论】:

  • $this-&gt;upload-&gt;do_upload() 替换为 $this-&gt;upload-&gt;do_upload('gambar_produk')
  • 请查看来自codeigniter.com/userguide3/libraries/file_uploading.html的文件上传类文档
  • 请始终通过提供一些 cmets 来回应答案,或者,如果对您有帮助,请将其标记为绿色并点赞,这是感谢所有程序员的最佳方式

标签: php html codeigniter codeigniter-3


【解决方案1】:

希望对您有所帮助:

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

$this-&gt;upload-&gt;do_upload() 替换为$this-&gt;upload-&gt;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

【讨论】:

    【解决方案2】:

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

    要使其正常工作,请将输入文件的名称 gambar_produk 更改为 userfile 或使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2016-11-12
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多