1. 程式人生 > >PHP實現CSV檔案的建立並且直接下載

PHP實現CSV檔案的建立並且直接下載

<?php

function createcsv($fileName,$csv_body)
{
    // 頭部標題
    $csv_header = array('sku');
    $header = implode(',', $csv_header) . PHP_EOL;
    $content = '';
    foreach ($csv_body as $k => $v) {
        $content .= implode(',', $v) . PHP_EOL;
    }
    $csvData = $header . $content;
    header("Content-type:text/csv;");
    header("Content-Disposition:attachment;filename=" . $fileName);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo $csvData;
}

$a[] = array(1);
$a[] = array(1);
$a[] = array(1);
$a[] = array(1);

createcsv('1.csv',$a);