1

So I am wanting to allow users to:

1. Upload a .csv file (mystudents.csv)
2. Import the rows in the .csv
3. Create a PHP array for all this
4. Insert this information into a database

I have the following code to upload the file and create the array.

public function studentImport()
{
    ini_set('auto_detect_line_endings', TRUE);

    $config['upload_path'] = './static/files/importFiles';
    $config['allowed_types'] = '*';
    $config['max_size'] = '800';

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

    if ( ! $this->upload->do_upload())
    {
        var_dump($this->upload->display_errors());
    }
    else
    {
        $data = $this->upload->data();

        $file_path = './static/files/importFiles/'.$data['file_name'];

        $rows = array_map('str_getcsv', file($file_path));
        if($rows){
            $header = array_shift($rows);
            $csv = array();
            foreach($rows as $row) {
                $csv[] = array_combine($header, $row);
            }

            var_dump($csv);
        }
    }
}

The file gets uploaded just fine, but the errors happens with the line: $rows = array_map('str_getcsv', file($file_path));

And that error is:

array_map() [function.array-map]: The first argument, 'str_getcsv', should be either NULL or a valid callback

What do I need to do to make this work out?

1 Answer 1

1

looks like the function str_getcsv is not available

Test it with

if (function_exists('str_getcsv')) {
  print "str_getcsv defined\n";
} else {
  print "str_getcsv not defined\n";
} 

if function exists try

$csv= file_get_contents($file_path);
$array = str_getcsv($csv);
print json_encode($array);  

or

$csv= file_get_contents($file_path);
$array = array_map('str_getcsv', explode("\n", $csv));
print json_encode($array);  
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.