Deepak Keynes
Deepak Keynes

Reputation: 2329

You didnot select a file to upload error in codeigniter

I am getting error like "You did not select a file to upload." It is a weird issue and I am not aware of where I am getting wrong in my code.

My controller code is below:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends MY_Controller {

public function do_upload()
        {       
                ini_set("display_errors",1); // I added so it would help me show errors
                error_reporting(E_ALL);

                //print_r($this->input->post());exit(); // post result shows "submit = submit"
                $config['upload_path']          = '/assets/uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 10000;
                //$config['max_width']            = 1024;
                //$config['max_height']           = 768;
                //print_r($config);exit();
                $this->load->library('upload', $config);
                $this->upload->initialize($config);

                if ( ! $this->upload->do_upload())
                {
                        $error = array('error' => $this->upload->display_errors());
                        $this->load->view('login/header');
                        $this->load->view('login/dashboard/add_logo', $error);
                        $this->load->view('login/footer');
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

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

I am just trying to upload a logo in my page. I am not storing into a database so no model now. My view code is here below:

<div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="col-md-12">
                            <div class="header">
                                <h4 class="title">Header/Footer Logo</h4>

                            </div>
                            </div>
                            <div class="col-md-12">

                            <form action="<?php echo site_url();?>/dashboard/do_upload" role="form" method="post" enctype="multipart/form-data">
                            <div class="panel">
                                <div class="panel-body">
                                    <b><?php if(isset($error)) echo $error; ?></b>
                                    <div class="form-group">
                                        <label>Logo</label>
                                        <input class="form-control" type="file" name="picture" />
                                    </div>
                                     <div class="form-group">
                                        <input type="submit" class="btn btn-warning" name="submit" value="submit">
                                    </div>
                                </div>
                            </div>
                          </form>
                          </div>
                        </div>
                    </div>
                   </div>
            </div>
        </div>

Upvotes: 2

Views: 38

Answers (1)

Vickel
Vickel

Reputation: 7997

you missed to add the name attribute of your file input in your do_upload function.

you have: <input class="form-control" type="file" name="picture" />

so just do this:

if ( ! $this->upload->do_upload('picture')){
  // your code
}

Upvotes: 2

Related Questions