0

I want to import CSV file in MySQL using CodeIgniter, I am defined names and right functions(hope so), but getting the undefined index of my file I m finding solutions and also getting one but won't work.

Controller

 $file_data = $this->csvimport->get_array($_FILES['csvfile']['name']);
        foreach($file_data as $row)
        {
            $data[] = array(
            'Hall_Ticket_No' => $row['Hall_Ticket_No'],
            'Name'  => $row['Name'],
            'Course'   => $row['Course']
           );
        }
        $data['query'] = $this->ExamModel->insertBlock($data);

HTML

    <div class="form-group col-lg-12 col-xs-12">
                        <label for="csv_file">Select excle (CSV) file:</label>
                        <input type="file" name='csvfile' accept=".csv" class="form-control" id="csv_file" required="">
                    </div>

Error

   Severity: Notice

   Message: Undefined index: csvfile

    Filename: controllers/Exam.php

    Line Number: 20

Erros image

Touheed Khan
  • 2,149
  • 16
  • 26

3 Answers3

3

Make sure that in your form.. you put the enctype. eg:

<form method="post" enctype="multipart/form-data" action="index.php"></form>

To check if files are successfully updated upon submitting the form. use print_r to see results.

print_r($_FILES);
Maresh
  • 4,644
  • 25
  • 30
0
<form action="{UPLOAD-URL}" method="post" enctype="multipart/form-data">
    <div class="form-group col-lg-12 col-xs-12">
      <label for="csv_file">Select excle (CSV) file:</label>
      <input type="file" name='csvfile' accept=".csv" class="form-control" id="csv_file" required="">
      <input type="submit" value="Upload Image" name="submit">
                    </div>
    <input type="submit" value="Upload Image" name="submit">
</form>

Try above code by replacing {UPLOAD-URL} which works for me. Basically I have added form element with enctype attribute to your code.

Using JS

function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
var f = evt.target.files[0]; 

if (f) {
  var r = new FileReader();
  r.onload = function(e) { 
      var contents = e.target.result;
    alert( "Got the file.n" 
          +"name: " + f.name + "n"
          +"type: " + f.type + "n"
          +"size: " + f.size + " bytesn"
          + "starts with: " + contents.substr(1, contents.indexOf("n"))
    );  
  }
  r.readAsText(f); //Reading the file

   //Add AJAX code here to submit the file

 } else { 
  alert("Failed to load file");
 }
}

document.getElementById('csvfile').addEventListener('change', readSingleFile, false);
Sathishkumar Rakkiyasamy
  • 3,509
  • 2
  • 30
  • 34
0

This error could happen if the CSV file is formatted as "UTF-8 with BOM". Open the CSV file in Notepad and see the formatting in the bottom right corner.

If creating the CSV files with Excel, make sure when saving to use the "CSV (MS-DOS) (*.csv)" option and NOT the "CSV (UTF-8)".

See Whats the difference between utf-8 and utf-8 without bom

Simen
  • 1