How to Create and Read CSV Files with PHP

there is a function in php called fputcsv which helps us create a csv file.


$csv =fopen('example.csv','w+');



as the first parameter, it gets the file we opened with fopen, and for the second one, we put the array we want to put into the CSV file.

it will be an array of arrays. each array represents a row in the CSV file. the first one is the header.

you can set the header and footer separately too.

$header = ['fname','lname','address','email'];


$records = [];
foreach ($records as $record){


and at last, we have to close the file with fclose() function.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *