3

I want to upload a file using ajax, based colaborated of codeigniter and file input kartik.

So I created like this :

<label for="file">Upload CSV 's</label>
<input name="egt" id="egt" class="file-loading" type="file" multiple data-no="1">

JS

$("#egt").fileinput({
        dropZoneEnabled: false,
        uploadUrl: "<?php echo site_url('admin/kecil/c_daily_egt/create_daily/egt') ?>",
        uploadAsync: true,
        maxFileCount: 20,
    });

Codeigniter

public function create_daily() {
    $pathToUpload = "./assets/uploads/";
    $dir_exist = true; // flag for checking the directory exist or not
    if (!is_dir($pathToUpload)) {
        mkdir($pathToUpload, 0777, true);
        $dir_exist = false; // dir not exist
    }


    $name_file = $_FILES['egt']['name']; //initialize name of file

    $config['upload_path'] = $pathToUpload;
    $config['file_name'] = $name_file;
    $config['allowed_types'] = 'csv';
    $config['file_name'] = '';
    $config['max_size'] = '50000';
    $config['overwrite'] = TRUE;

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

    if (!empty($_FILES)) {
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($name_file)) {
            echo json_encode($this->upload->display_errors());
        } else {
            $upload_data = $this->upload->data();
            echo json_encode("Success" . $upload_data);
        }
    }
}

I just got error like this :

You did not select a file to upload.

So, i debug it like this :

Array
(
[egt] => Array
    (
        [name] => EGT_STRG.CSV
        [type] => application/vnd.ms-excel
        [tmp_name] => E:\wamp64\tmp\phpE56C.tmp
        [error] => 0
        [size] => 4748
    )
)

Any help it so appreciated.

2
  • any js error in console? Commented Sep 3, 2016 at 10:29
  • refer stackoverflow.com/questions/4731071/… Commented Sep 3, 2016 at 10:37

1 Answer 1

0

You are referencing to the file by filename:

if (!$this->upload->do_upload($name_file)

But you need to reference to the 'egt' which is the file you are uploading.

Try this:

if (!$this->upload->do_upload('egt')
Sign up to request clarification or add additional context in comments.

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.