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.

Example:

<?php

$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);
}

fclose($fp);
?>

Delete CSV file:

<?php
unlink('file.csv');
?>

Note: rmdir() function removes directory.

Leave a Reply

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