Importing Excel (CSV) files with PHP

How to import your multiple column rows with PHP, simple and a clean way to loop through your .CSV and excel data sheets.

import excel csv with php

Importing Excel (CSV) files with PHP

To import your Excel files in PHP you must first save them as a CSV file. When you have exported and created your .CSV file we will than be able to import it in PHP. In this case we will be using the fopen function from php to open the existing file.

When you will open this file in PHP make sure that you always provide the correct location of the file. In this case we will be keeping it in our main directory to keep things easy. When doing this on a web- hosting your main folder name mostly will be named something like ‘public_html’ or ‘private_html’ depending on your HTTPS settings.

When you place your .php and .csv file in the same location PHP will be easily able to locate the file’s path.

Creating a While CSV PHP Loop

Next up we are going to loop through our datasheet from excel (CSV), in this case we have 2 rows which have the key 0 and 1. Data array’s always start at 0 for the first result and key 1 for the next and so on. This means that for each row we have 2 values which are ‘Name of the company’ and ’Phone number of the company’ as declared in the image.

CSV Import to PHP

// Here we check with fopen if the bestandsnaam.csv file is available in the same directory and give it the r value which means READ. In the same time we setup $handle with the fopen values.

if (($handle = fopen(“bestandsnaam.csv”, “r”)) !== FALSE) { 
    // Next we are going to ask the script if it is true that $data = fgetcsv is true that it has more than 1000 bytes and split them up by the comma (the way csv files are saved).
    while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
        $num = count($data); // The first result for each row we are going to safe as phonenumber. ($nummer)
        $row++; // We make the row count up for each result +1.
        for ($c=0; $c < $num; $c++) { // Setup the variable $c as count to 0 so we can check if it is the first or the second column in the CSV. After this result we will +1 the same result in order to make it go to the next column.
           if ($c == 0) {
                // We check if the count ($c) is 0 which in the first case is always true, this is how we will be saving the first variable.

                // When you place $variable = “test1”; than you are assigning the $variable to a new value. In this case if you do $variable .= “test2”; after you already defined the variable before, it will add the string to the variable. In this case the output of $variable will now be: “test1test2”.

                //So for the first row each time it will be adding the value of the row to $namen since the first row are the names. If this count $c value is not 0 than in this case it is not the first row and it will continue to the else function.

                $namen .= $data[$c] . “,”;

            } else {

                // So when $c != 0 then in that case it will be the second, third and so on. In this case the second row is the phone numbers so we will be adding these to a variable.
                $nummers .= $data[$c] . “,”;
    fclose($handle); // Here we fclose the file to correctly close the handle with PHP.
    $namen = explode(‘,’,$namen); // Convert the names from the variable to an array and split them by comma
    $nummers = explode(‘,’,$nummers); // Also the numbers should be converted to an array and split.

    // Now we are going to loop through our created arrays to add a function to each row.
    for ($i = 0; $i < count($namen); $i++) {

// For $i = 0, when the $i is lower than the total of $names in the array add +1 to the index count variable which is $i. This is to loop through the amount of counted names in our array.

        // Here we are going to print out each value for each count in the $names array. We will then print a result with the echo function. 
        echo ‘naam: ‘.$namen[$i].’ met nummer:’.$nummers[$i].'<br />’;

Placing .CSV rows in their own variables

Next up we are going to connect row’s to variables so we can assign the phone numbers to $nummers and the names to $namen. On this way we create 2 different arrays filled with the variables so we can use them later on. We do this by creating a loop with a variable to check and see if the count is 1 or 0.

After storing all the data from the CSV file to 2 different PHP variables we are going to close the opened file by calling fclose to correctly close the file with PHP. Now with the long stored PHP variables devided by comma’s we are going to create an array from it. We are going to do this by calling the explode function based on the comma. What we will be doing is the following:

Converting string from CSV to array

When you have a string that contains the following: “A,B,C,” this string will be divided and split into an array with 3 elements. For example, with array’s you can call each position that’s in the array. If you would like the second result in this case (the B) then you would be able to call that variable from the array by calling the exploded variable and then the key value of the variable you want. In this case I could find the 2nd number of all phone numbers stored by calling $nummers[1] in PHP.

On this matter you can extract data from an array and also use a foreach or while loop to add other functions to each row.

Subscribe to our newsletter

Want to get frequent notifications about the posts on our blog? Fill in your details with the options we provide and then we will handle the rest for you!