In this tutorial, you will learn how to import and validate a CSV file in PHP. Here, we process a CSV file uploaded from a form and parse it.

If you don’t know how to export a CSV file, you can follow this tutorial: How to Export Data to CSV File using PHP

In this tutorial, we will import the file that we export in our previous tutorial. The steps are as follows:

Let’s create a form with file input and a submit button. Here, we leave form action attribute to an empy string. So, it submits the form to the same file.

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <button type="submit" name="import">Import</button>
</form>

Next, we check for post request from a form and also the file.

if(isset($_POST['import'])) {

    if((isset($_FILES['file']) && is_array( $_FILES['file']))) {

        $csv = $_FILES['file'];

        if(isset($csv['tmp_name']) && !empty($csv['tmp_name'])) {

        }
    }
}

Now, we validate the file for correct mime type.

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $csv['tmp_name']);
finfo_close($finfo);

$allowed_mime = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');

if(in_array($mime, $allowed_mime) && is_uploaded_file($csv['tmp_name'])) {

}

Here, we also use is_uploaded_file() function to check if the file is uploaded via HTTP POST.

Next, open the file using fopen() with read mode.

$f = fopen($csv['tmp_name'], 'r');

Suppose the file that we upload contains the following CSV entries.

name,salary
Jane,2500
Paul,2000
Jake,3000
Tim,3500

Here, the first row contains two columns which we don’t need to parse. So, we can skip the row.

fgetcsv($f);

Now, initialize an empty array where we will store the data from CSV file.

$data = array();

We can use while loop to parse each row and push this row to the array.

while($row = fgetcsv($f)) {
    $name = $row[0];
    $age = $row[1];
    array_push($data, array($name, $age));
}

Lastly, close the file.

fclose($f);

So, now we have the data from the CSV file.

echo '<pre>';
var_dump($data);
echo '<pre>';
die;

The code for importing a CSV file is as follows:

<?php
/*
CSV file: employees.csv
-------------
name,salary
Jane,2500
Paul,2000
Jake,3000
Tim,3500
-------------
*/

if(isset($_POST['import'])) {

    if((isset($_FILES['file']) && is_array( $_FILES['file']))) {

        $csv = $_FILES['file'];

        if(isset($csv['tmp_name']) && !empty($csv['tmp_name'])) {

            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $csv['tmp_name']);
            finfo_close($finfo);

            $allowed_mime = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');

            if(in_array($mime, $allowed_mime) && is_uploaded_file($csv['tmp_name'])) {

                $f = fopen($csv['tmp_name'], 'r');

                fgetcsv($f);

                $data = array();

                while($row = fgetcsv($f)) {
                    $name = $row[0];
                    $age = $row[1];
                    array_push($data, array($name, $age));
                }

                fclose($f);

                echo '<pre>';
                var_dump($data);
                echo '<pre>';
                die;
            }
        }
    }
}
?>

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <button type="submit" name="import">Import</button>
</form>

Leave a comment

Your email address will not be published. Required fields are marked *