Home > Backend Development > PHP Tutorial > How to import CSV file in PHP?

How to import CSV file in PHP?

WBOY
Release: 2023-09-03 15:38:01
forward
1257 people have browsed it

How to import CSV file in PHP?

The following code can be used to import a CSV file in PHP -

<?php
$row = 1;
if (($handle = fopen("name_of_file.csv", "r")) !== FALSE) {
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      $num = count($data);
      echo "<p> $num fields in line $row: <br /></p></p><p>";
      $row++;
      for ($c=0; $c < $num; $c++) {
         echo $data[$c] . "<br /></p><p>";
      }
   }
   fclose($handle);
}
?>
Copy after login

This file will display the contents of the csv file on the screen.

In the above code, starting from line 1 (because line 0 usually contains headers/column names), the csv file is opened in read mode and the fgetcsv function is called to read the 1000 rows of data.

The number of columns in each row is displayed along with its content.

p>

The above is the detailed content of How to import CSV file in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template