我想创建一个csv文件,但是当我运行代码时,它返回一个空白页,没有csv文件。我使用PHP5。我使用以下代码:
<?php $data = array ('aaa,bbb,ccc,dddd', '123,456,789', '"aaa","bbb"'); $fp = fopen('data.csv', 'w'); foreach($data as $line){ $val = explode(",",$line); fputcsv($fp, $val); } fclose($fp); ?>
谢谢!
其空白,因为您正在写信file。您应该写到outputusing php://output代替,还应该发送标头信息以表明它是csv。
file
output
php://output
例
header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="sample.csv"'); $data = array( 'aaa,bbb,ccc,dddd', '123,456,789', '"aaa","bbb"' ); $fp = fopen('php://output', 'wb'); foreach ( $data as $line ) { $val = explode(",", $line); fputcsv($fp, $val); } fclose($fp);