How to create CSV file in PHP?
PHP Trending Tech

How to create CSV file in PHP?

Create CSV file:

PHP fputcsv function help us for creating CSV file. It returns the length of the written string or false on failure.



$list = array (
    array('ID', 'Name', 'Email', 'Roll Number'),
    array('1', 'Amit', '789'),
    array('2', 'Gaurav', '790')

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);


Delete CSV file:


Note: rmdir() function removes directory.

Leave a Reply

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