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

7
  • at what line do you get the error? Commented Feb 13, 2018 at 11:59
  • @Jeff errors added Commented Feb 13, 2018 at 12:02
  • and which line is line 20...? Commented Feb 13, 2018 at 12:02
  • @Jeff $file_data = $this->csvimport->get_array($_FILES['csvfile']['name']); Commented Feb 13, 2018 at 12:04
  • But I doubt the shown first line is working as expected. Commented Feb 13, 2018 at 12:04

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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

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.