Free cookie consent management tool by TermsFeed Generator PHP CSV Files | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP CSV Files

PHP CSV Files

Last Updated on Mar 21, 2023

Intro

Do you remember working with files? Then let's get started

Read CSV

reading csv is like reading a file but instead of reading the line with fgets we use fgetcsv to read the line

fgetcsv(file, length, separator, enclosure);

File: is the file we have opened

Length: 0 for reading the whole row or you can limit how much of the row it should read

Separator: for csv file the default is comma “,”

Enclosure: for quotes the default is double quotes ‘“‘

/* the csv file looks like this
id,name
1,Amir
2,Pratham
3,Simon
*/
$file = fopen('example.csv','r');
//fgetcsv(file, length, separator, enclosure, escape);
$row = fgetcsv($file);
while($row != false){
    echo $row[0] . '-' . $row[1];
    echo '<br>';
    $row = fgetcsv($file);
}
fclose($file);
/* output is :
id-name
1-Amir
2-Pratham
3-Simon
 */

Write CSV

Like writing to a file but instead of using fwrite we use the fputcsv function. The first arg is the file and the second arg is all the columns of that row as an array.

It will take care of the new line and enclosing the string and everything else. Super easy!

$firstRow = ['id','name'];
$names = [
    ['1','Amir'],
    ['2','Pratham'],
    ['3','Simon'],
];
$file = fopen('example.csv','w');
fputcsv($file,$firstRow);
foreach($names as $name){
    fputcsv($file,$name);
}
fclose($file);

Here writing and reading the file at once

// write to a csv
$firstRow = ['id','name'];
$names = [
    ['1','Amir'],
    ['2','Pratham'],
    ['3','Simon'],
];
$file = fopen('example.csv','w');
fputcsv($file,$firstRow);
foreach($names as $name){
    fputcsv($file,$name);
}
fclose($file);
/* the csv file looks like this
id,name
1,Amir
2,Pratham
3,Simon
*/

// read the csv file
$file = fopen('example.csv','r');
//fgetcsv(file, length, separator, enclosure, escape);
$row = fgetcsv($file);
while($row != false){
    echo $row[0] . '-' . $row[1];
    echo '<br>';
    $row = fgetcsv($file);
}
fclose($file);
/* output is :
id-name
1-Amir
2-Pratham
3-Simon
 */

https://youtu.be/_t8RhQRxHVA

Conclusion

Now you know about working with csv files in PHP.

I recommend you to open a PHP files and try write to a csv file and read the data from the same file.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • what are csv files
  • read csv
  • write csv

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Static
Mar 22, 2023 programming

PHP Static

Today we are going to talk about Static methods and properties in PHP. You can access the methods and properties of a class without create a new object but for that you need to know about static. ...

5 Min Read Read More
PHP compact
Feb 14, 2023 programming

PHP compact

Today we are going to talk about compact function in PHP. Compact is one of those functions that helps the readability of your code a lot and also reduces the lines of code. ...

4 Min Read Read More
PHP Validate URL
Feb 15, 2023 programming

PHP Validate URL

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the URLs given by the user to make sure the address has a correct format. ...

4 Min Read Read More
PHP Callback Functions
Jan 08, 2023 programming

PHP Callback Functions

Today we are going to talk about callback functions in PHP. sometimes we need to pass a function as an argument of another function, that's exactly what callback function is. ...

7 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...