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); } ?>
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!