Member Avatar for anmol.raghuvanshi1
anmol.raghuvanshi1

I am following this tutorial

but i am facing problem in uploading file i done similar as in tutorial.Below is my code
Code:

<?php echo form_open_multipart('upload/do_upload', array('class' => 'upload-image-form'));?> <div class="form-inline"> <div class="form-group"> <input type="file" name="uploadfile[]" id="js-upload-files" multiple = "multiple" accept = "image/*"> <br /> </div> <button type="submit" value="upload" class="btn btn-sm btn-primary" id="js-upload-submit">Upload files</button> </div> <script>                   
            jQuery(document).ready(function($) {

                var options = {
                    beforeSend: function(){
                        // Replace this with your loading gif image
                        $(".upload-image-messages").html('<p><img src = "<?php echo base_url() ?>img/images.png" class = "loader" /></p>');
                    },
                    complete: function(response){
                        // Output AJAX response to the div container
                        $(".upload-image-messages").html(response.responseText);
                        $('html, body').animate({scrollTop: $(".upload-image-messages").offset().top-100}, 150);

                    }
                }; 
                // Submit the form
                $(".upload-image-form").ajaxForm(options); 

                return false;

            });
            </script> </form>

// My controller Upload.php

<?php
class Upload extends CI_Controller {

    public function __construct() {

        parent::__construct();
        // Load the helpers
        $this->load->helper(array('form', 'url'));
    }

    public function index() {

        // Load the form
        $this->load->view('header');
        $this->load->view('registration_continue', array('error' => ' ' ));
        $this->load->view('footer');

    }

    public function do_upload(){

        // Detect form submission.
        if($this->input->post('submit')){



            $path = './upload/large/';
            $this->load->library('upload');


            // Define file rules
            $this->upload->initialize(array(
                "upload_path"       =>  $path,
                "allowed_types"     =>  "gif|jpg|png",
                "max_size"          =>  '2048',
            ));

            if($this->upload->do_multi_upload("uploadfile")){

                $data['upload_data'] = $this->upload->get_multi_upload_data();

                echo '<p class = "bg-success">' . count($data['upload_data']) . 'File(s) successfully uploaded.</p>';


            } else {   
                // Output the errors
                $errors = array('error' => $this->upload->display_errors('<p class = "bg-danger">', '</p>'));              

                foreach($errors as $k => $error){
                    echo $error;
                }

            }

        } else {
            echo '<p class = "bg-danger">An error occured, please try again later.</p>';

        }
    die(var_dump($_FILES['uploadfile']));
        // Exit to avoid further execution
        exit();
    }
}

var dump result

array (size=5)
  'name' => 
    array (size=1)
      0 => string '8.jpg' (length=5)
  'type' => 
    array (size=1)
      0 => string 'image/jpeg' (length=10)
  'tmp_name' => 
    array (size=1)
      0 => string 'C:\xampp\tmp\phpA125.tmp' (length=24)
  'error' => 
    array (size=1)
      0 => int 0
  'size' => 
    array (size=1)
      0 => int 57203

I have also one small doubt what is use of routing here

$route['upload/do_upload'] = 'upload/do_upload';
$route['upload/(:any)'] = 'upload/view/$1';
$route['upload'] = 'upload'

I have folder upload in my project folder
and url of my upload is http://localhost/ko/Camping/next_page
Well i am learning ajax so help
Any help...