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

How to import CSV file in PHP?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-03 15:38:01
forward
1293 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:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template