This tutorial shows how to read from a csv file and write to a csv file using PHP programming language. In this example I have shown how to read a csv file and display those values on the browser when you run the PHP file. I have also written those csv entries into another output csv file.
There are few advantages of CSV data:
- CSV format is considered to be standard format
- CSV is smaller in size and faster to handle
- CSV is simple to implement and easy to parse
- CSV is human readable and easy to edit manually
- CSV is processed by almost all applications
Prerequisites
PHP 7.4.3
CSV File Reading Example
In the below code snippets, first I check if the csv file exists and has required permission for reading.
Then you need to iterate for each line and print the data using array index. The fgetcsv()
function retrieves comma separated values into array from each line.
$file_name = "read.csv";
if (($handle = fopen($file_name, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
echo $data[0] or $data[1] or $data[2] etc.
//more code
}
fclose($handle);
}
CSV File Writing Example
The below code snippets tell that we first open a file with write mode. Make sure that the file is accessible and has the required permission for writing to it.
Then we write the comma separated line to csv file using fputcsv()
function.
$fp = fopen('write.csv', 'w');
//$infos array containing comma separated values
foreach ($infos as $info) {
fputcsv($fp, array($info));
}
fclose($fp);
Sample CSV Data
Here is the sample csv data which I am going to read, display and write to another csv file.
abc xyz,bdhur@mail.com,20/05/1874
abe xuz,sumaut@mail.com,05/04/1987
dfa jyz,akash@hr.com,04/07/1864
fff hgh,arya.p@tech.com,05/05/1905
Read and Write CSV File
Here in the below code snippets that will read from csv file and display those content from csv file into HTML table on the browser. At the same time the below code writes the csv content into another csv file.
<?php
$infos = array();
$file_name = "read.csv";
if (($handle = fopen($file_name, "r")) !== FALSE) {
echo '<table border="1"><thead>';
echo '<th>Name</th><th>Email</th><th>Date Of Birth</th>';
echo '</thead>';
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
echo '<tr><td>' . $data[0] . '</td><td>' . $data[1] . '</td><td>' . $data[2] . '</td></tr>';
$infos[] = $data[0] . ',' . $data[1] . ',' . $data[2];
}
echo '</tbody></table>';
fclose($handle);
}
$fp = fopen('write.csv', 'w');
foreach ($infos as $info) {
fputcsv($fp, array($info), ',', ' ');
}
fclose($fp);
/*
* End of php-read-write-csv-file.php
*/
Output CSV File
The below content is the output of the csv content written into file.
abc xyz,bdhur@mail.com,20/05/1874
abe xuz,sumaut@mail.com,05/04/1987
dfa jyz,akash@hr.com,04/07/1864
fff hgh,arya.p@tech.com,05/05/1905
Output on Browser
Hitting the URL http://localhost/php-read-write-csv-file/php-read-write-csv-file.php on browser will give you the following output:

I have a scenario wherein admns from other departments can upload a (csv) file to server. Data structure is
name,date,comments. Here in the details column, there may be many occurrences of comma.
Example.
Tom,3/12/2017, Logged the data, ordered 14 pieces
As you can see there is comma in the 3rd field.
As a super user, I need to read the csv file,check this column and replace comma with another char and then load in mysql db.
Can you tell me how to do?
split each line by first two comma only, then you will be able to get the third column with comma.