2

I am receiving an error stating You did not select a file to upload. when trying to upload a file, though I have chosen a file to upload. I am using an input file that was style using bootstrap. Is the issue with that? I followed the instructions in the Codeigniter user guide for file uploads. Can anyone please help me and identify the error in my code?

Thank you

Controller

public function create()
{
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';

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

    if ( ! $this->upload->do_upload('userfile'))
    {
            echo "Upload failed";
    }
    else
    {
            $data = array('upload_data' => $this->upload->data());

    }

    $data = array(
        'category' => $this->input->post('category');
        'name' => $this->input->post('recipename'),
        'ingredients' => $this->input->post('ingredients'),
        'directions' => $this->input->post('directions'),
        'date' => date("Y/m/d")

    );

    $this->model->insert($data);
    redirect('', 'refresh');
}

View

<form action="<?php echo base_url();?>home/create" method="POST" role="form" multipart>
        <legend>New Recipe</legend>

        <div class="form-group">
            <label for="">Category</label>

            <select name="category" id="input" class="form-control">
                <?php foreach ($category as $value) {?>
                <option value="<?php echo $value->category; ?>"><?php echo $value->category;?></option>
                <?php } ?>

            </select>


            <label>Name</label>
            <input type="text" name="recipename" class="form-control" id="" placeholder="Recipe Name">
            <label>Image</label>
            <div class="input-group">
                <label class="input-group-btn">
                    <span class="btn btn-primary">
                        Browse&hellip; <input type="file" name="userfile" style="display: none;" multiple>
                    </span>
                </label>
                <input type="text" class="form-control" readonly>
            </div>
            <label>Ingredients</label>
            <textarea name="ingredients" id="input" class="form-control" required="required"></textarea>
            <label>Directions</label>
            <textarea name="directions" id="input" class="form-control" required="required"></textarea>

        </div>



        <button type="submit" class="btn btn-primary">Submit</button>
      </form>

 <script type="text/javascript">


    $(function() {

      // We can attach the `fileselect` event to all file inputs on the page
      $(document).on('change', ':file', function() {
        var input = $(this),
            numFiles = input.get(0).files ? input.get(0).files.length : 1,
            label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
        input.trigger('fileselect', [numFiles, label]);
      });

      // We can watch for our custom `fileselect` event like this
      $(document).ready( function() {
          $(':file').on('fileselect', function(event, numFiles, label) {

              var input = $(this).parents('.input-group').find(':text'),
                  log = numFiles > 1 ? numFiles + ' files selected' : label;

              if( input.length ) {
                  input.val(log);
              } else {
                  if( log ) alert(log);
              }

          });
      });

    });
</script>
Beldion
  • 321
  • 8
  • 19
  • are you uploading file via ajax? – Bhunesh Satpada Jun 24 '17 at 12:43
  • No I am not. I am just using php. – Beldion Jun 24 '17 at 12:46
  • @Beldion Add this appropriate form attribute `enctype="multipart/form-data"` to View. in addition, input file element has multiple file selected but in your controller will not support multiple file upload check this to help you [link](https://stackoverflow.com/a/20138535/5589166) – Sovary Jun 24 '17 at 13:27

1 Answers1

5

use enctype in your html form tag.

<form action="<?php echo base_url();?>home/create" method="POST" role="form" enctype="multipart/form-data" >

now try . it will work .:)

Sunil Rajput
  • 960
  • 9
  • 19
  • It displaying a different error now, it says `The upload path does not appear to be valid` – Beldion Jun 24 '17 at 13:28
  • now your file reached to the controller but it cant find your directory to staore it . check https://stackoverflow.com/questions/6159851/uploading-an-image-in-codeigniter-shows-error-the-upload-path-does-not-appear-to – Sunil Rajput Jun 24 '17 at 13:32