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

how to use git restore and completely ignore the changes: 3 scenarios (video)
Nov 27, 2022 programming

how to use git restore and completely ignore the changes: 3 scenarios (video)

we are going to talk about ways to ignore the edits we made in a git project and restore the changes by git restore ...

9 Min Read Read More
PHP random number
Feb 15, 2023 programming

PHP random number

Today we are going to talk about generating random numbers in PHP. Knowing how to generate a random number is more useful than you think. you can select random users and much more. ...

3 Min Read Read More
What to Do After Learning Programming
Mar 17, 2024 programming

What to Do After Learning Programming

Congratulations! You've learned programming and now have a solid foundation in coding. But what should you do next? ...

7 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

28 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 ...