1

I'm making a form where the person could add a picture in it and it will show in a local directory folder and show up in sql table. But it seems that how many times that I tried the picture I tried uploading wont upload! Please help!

Controller:

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

                    }
                }

            }

The Input Type in the form has an ID of "gambar_produk" and also has a name of "gambar_produk". The function that I am trying to use is the do_upload function. I tried searching for various ways but it still won't work. Please help!

3
  • 1
    Relace it $this->upload->do_upload() with $this->upload->do_upload('gambar_produk') Commented Jul 30, 2018 at 9:01
  • please check the File Uploading Class documentation from codeigniter.com/userguide3/libraries/file_uploading.html Commented Jul 30, 2018 at 9:05
  • pls always response to the answers by giving some comments or ,if it helps you, by marking it as green and upvoting, it is the best way to thanks all the programmers Commented Jul 30, 2018 at 14:26

2 Answers 2

0

Hope this will help you :

Note : your form should have attribute enctype="multipart/form-data", better use form_open_multipart();

Relace $this->upload->do_upload() with $this->upload->do_upload('gambar_produk'), where gambar_produk is the name of file input

Your method do_upload should be like this :

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

    }
}

For more : https://www.codeigniter.com/userguide3/libraries/file_uploading.html

Sign up to request clarification or add additional context in comments.

Comments

0

By default do_upload expects the file to come from a form field called userfile, and the form must be of type “multipart”.

To make-it works, please change the name of the input file gambar_produk to userfile or use

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.