Import CSV, row by row, into custom table I have a Drupal 7 submit method for updating CSV row by row to a custom table which I found under this link. I have tried to update it so that it works in Drupal 8 to no avail. How do I to modify it to work in Drupal 8?

function custom_module_submit($form, &amp;$form_state) {

    $file = $form_state['values']['csv_upload_file'];
    $file-&gt;status = FILE_STATUS_PERMANENT;
    $file-&gt;filename = str_replace(' ', '_', $file-&gt;filename);
    file_save($file);

    $csv_file = file_load($file-&gt;fid);
    $file = fopen($csv_file-&gt;uri, "r");


    while(! feof($file))
    {
        $customer = fgetcsv($file));
        db_insert('your_db_table')
            -&gt;fields(array(
                'column1' =&gt; $customer[0],
                'column2' =&gt; $customer[1]
            ))
            -&gt;execute();
    }

    fclose($file);

    drupal_set_message('CSV data added to the database');
}


The Drupal 8 code that I am trying to use is as below:

public function submitForm(array &amp;$form, FormStateInterface $form_state)
{
    $file = $form_state-&gt;getValue('csv_upload');
    $file-&gt;status = FILE_STATUS_PERMANENT;
    $file-&gt;filename = str_replace(' ', '_', $file-&gt;filename);
    file_save_data($file);

    $csv_file = \Drupal\file\Entity\File::load($file-&gt;fid);
    $file = fopen($csv_file-&gt;getFileUri(), "r");

    while (!feof($file)) {
        $customer = fgetcsv($file);
        \Drupal::database()-&gt;insert('your_db_table')
            -&gt;fields(array(
                'column1' =&gt; $customer[0],
                'column2' =&gt; $customer[1]
            ))
            -&gt;execute();
    }

    fclose($file);

    drupal_set_message('CSV data added to the database');
}


Errors include:


Fatal error: Call to a member function getFileUri() on null in /home/annuities/public_html/modules/custom_module/src/Form/C‌​ustomForm.php on line 90. 


Line 90: $file = fopen($csv_file-&gt;getFileUri(), "r");


Warning: Creating default object from empty value in Drupal\custom_module\Form\CustomForm->submitForm() (line 85 of modules/custom_module/src/Form/CustomForm.php).


Line 85: $file-&gt;status = FILE_STATUS_PERMANENT;


Notice: Undefined property: stdClass::$filename in Drupal\custom_module\Form\CustomForm->submitForm() (line 86 of modules/custom_module/src/Form/CustomForm.php).


Line 86: $file-&gt;filename = str_replace(' ', '_', $file-&gt;filename);


The file could not be created.
Notice: Undefined property: stdClass::$fid in Drupal\custom_module\Form\CustomForm->submitForm() (line 89 of modules/custom_module/src/Form/CustomForm.php).


Line 89: $csv_file = \Drupal\file\Entity\File::load($file-&gt;fid);


Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 227 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
I used the code below;

public function submitForm(array &amp;$form, FormStateInterface $form_state)
{

    $file = $form_state-&gt;getValue('csv_upload_file');

    $destination = $file[0]-&gt;toArray()['uri'][0]['value'];

    $file = fopen($destination, "r");

    while (!feof($file)) {
        $customer = fgetcsv($file);
        \Drupal::database()-&gt;insert('your_db_table')
            -&gt;fields(array(
                'column1' =&gt; $customer[0],
                'column2' =&gt; $customer[1]
            ))
            -&gt;execute();
    }

    fclose($file);

    drupal_set_message('CSV data added to the database');
}