Open In App
Related Articles

How to Display Data from CSV file using PHP ?

Improve Article
Save Article
Like Article

We have given the data in CSV file format and the task is to display the CSV file data into the web browser using PHP. To display the data from CSV file to web browser, we will use fgetcsv() function.

Comma Separated Value (CSV) is a text file containing data contents. It is a comma-separated value file with .csv extension, which allows data to be saved in a tabular format.

fgetcsv() Function: The fgetcsv() function is used to parse a line from an open file, checking for CSV fields.

Execution Steps:

  • Open XAMPP server and start apache service

  • Open notepad and type the PHP code and save it as code.php

  • Store the CSV file in the same folder. Like xampp/htdocs/gfg/a.csv
  • Go to browser and type http://localhost/gfg/code.php. 


Filename: code.php


<!DOCTYPE html>
        <h3>Student data</h3>
        echo "<html><body><center><table>\n\n";
        // Open a file
        $file = fopen("a.csv", "r");
        // Fetching data from csv file row by row
        while (($data = fgetcsv($file)) !== false) {
            // HTML tag for placing in row format
            echo "<tr>";
            foreach ($data as $i) {
                echo "<td>" . htmlspecialchars($i
                    . "</td>";
            echo "</tr> \n";
        // Closing the file
        echo "\n</table></center></body></html>";


Last Updated : 14 Dec, 2020
Like Article
Save Article
Similar Reads
Related Tutorials