PHP file operation example: reading CSV file

PHPz
Release: 2023-06-20 11:44:01
Original
1987 people have browsed it

PHP is a popular programming language widely used in web development. In web applications, file operations are a basic and common function. This article will explain how to use PHP to read a CSV file and display it in an HTML table.

CSV is a common file format used to import tabular data into spreadsheet software such as Excel. CSV files usually consist of many lines, each line consisting of comma separated values. The first line usually contains column headers, which describe the meaning of each column value.

Here we will use the PHP built-in function fgetcsv() to read the CSV file. The fgetcsv() function reads and parses a row of data from a CSV file and puts it into an array.

First, we need to prepare a CSV file, which should meet the following requirements:

· Contains comma-separated columns

· Contains column headers

· File encoding is utf-8

For demonstration purposes we will use the following CSV file (file.csv):

name,age,gender

John,25, Male

Jane,22,Female

Bob,30,Male

Now we need to write PHP code to read the CSV file. Note that this code assumes your CSV file is in the same directory as your PHP file.

//Open CSV file

$file = fopen('file.csv', 'r');

/ / Read the first row (column header)

$headers = fgetcsv($file);

// Create an empty array to save the data

$data = array ();

// Loop through each row of data in the remaining part of the file

while (($row = fgetcsv($file)) !== FALSE) {

// Store row data into the data array

$data[] = $row;

}

// Close the file

fclose($ file);

// Output HTML table

echo "

// Output table column header

echo "< tr>";

foreach ($headers as $header) {

echo "

}

echo "

// Output table data

foreach ($data as $row) {

echo "

foreach ($row as $cell) {

echo "$cell";
Copy after login

}

echo "

}

echo "

"; "; "; " ; ";
$header
";

?>

Explain the above code. First, we open the CSV file and read the first row (column headers) using the fgetcsv() function. We store this in the $headers array.

Next, we use a loop to iterate through each line of data in the remainder of the file. Each row of data is parsed by the fgetcsv() function and stored in the $data array.

Then, we close the file and output the HTML table. We use the $headers array to output the column headers of the table, and the $data array to output the data of the table.

Now you can copy the above code into a PHP file and run it on your web server. You will see an HTML table containing CSV data that looks like this:

name age gender

John 25 Male
Jane 22 Female
Bob 30 Male

Summary

In the process of web development, file operation is a basic and common function. Using PHP, you can easily read and manipulate a variety of file types, including CSV, a widely used format for data import and export. Using the fgetcsv() function, you can read a CSV file into PHP and convert it into a data format suitable for display in a web application.

The above is the detailed content of PHP file operation example: reading CSV file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!