如何使fputcsv“echo”数据 [英] How to make fputcsv "echo" the data
本文介绍了如何使fputcsv“echo”数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一种方法让 fputscv
函数写数据到浏览器on-the-fly而不是创建一个临时文件,保存数据到该文件,并做一个 echo file_get_contents()
。帮助!!!
I need a way to make the fputscv
function write data to the browser on-the-fly instead of creating a temporary file, saving data into that file and doing a echo file_get_contents()
. Help!!!
推荐答案
在PHP文档网站上找到这个,首先在函数引用下注释:
Found this on the PHP docs website, first comment under the function reference:
function outputCSV($data) {
$outstream = fopen("php://output", 'w');
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals, ';', '"');
}
array_walk($data, '__outputCSV', $outstream);
fclose($outstream);
}
第二个选项:
And a second option:
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
fputcsv($csv, array('blah','blah'));
rewind($csv);
// put it all in a variable
$output = stream_get_contents($csv);
希望这有助于!
当试图弄清事物时,PHP文档应始终是您的第一站: - )
BTW the PHP docs should always be your first stop when trying to figure things out. :-)
这篇关于如何使fputcsv“echo”数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文