Create CSV File with PHP

前端 未结 2 472
执念已碎
执念已碎 2020-12-12 06:53

I want to create a new .csv file (without opening the raw file first via fopen). So far I have tried this:

$list[] = array
(
    "Name&q         


        
2条回答
  •  时光说笑
    2020-12-12 07:49

    You can use below code

    $list[]=array ("name","gender","age"); // push header here
    $list[] = array("John","M","21"); // push record here
    
    $timestamp0     = date("Y-m-d H:i:sa",time());
    $datetime       = new DateTime($timestamp0);
    $datetime->setTimezone(new DateTimeZone('Asia/Jakarta'));
    $timestamp      = $datetime->format("Y-m-d_H-i");
    
    $filename = __DIR__ . "/file/" . $timestamp . ".csv";
    
    $fp = fopen($filename, 'w');
    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);
    

    For more detail read php manual about CSV file. http://php.net/manual/en/function.fputcsv.php

提交回复
热议问题