如何从php脚本创建和下载csv文件?
我是一名新手程序员,我搜索了很多有关我的问题的信息,但找不到有关此问题的有用解决方案或教程.
I am a novice programmer and I searched a lot about my question but couldn't find a helpful solution or tutorial about this.
我的目标是我有一个PHP数组,并且数组元素显示在页面上的列表中.
My goal is I have a PHP array and the array elements are showing in a list on the page.
我想添加一个选项,以便用户想要的话,他/她可以创建带有数组元素的CSV文件并下载.
I want to add an option, so that if a user wants, he/she can create a CSV file with array elements and download it.
我不知道该怎么做.我也搜索了很多.但尚未找到任何有用的资源.
I don't know how to do this. I have searched a lot too. But yet to find any helpful resource.
请自行提供一些教程或解决方案或建议.我是新手,请提供易于实施的解决方案.
Please provide me some tutorial or solution or advice to implement it by myself. As I'm a novice please provide easy to implement solutions.
我的数组如下:
Array
(
[0] => Array
(
[fs_id] => 4c524d8abfc6ef3b201f489c
[name] => restaurant
[lat] => 40.702692
[lng] => -74.012869
[address] => new york
[postalCode] =>
[city] => NEW YORK
[state] => ny
[business_type] => BBQ Joint
[url] =>
)
)
您可以使用内置的 fputcsv()用于数组,以便从数组中生成正确的csv行,因此您必须循环并收集行.像:
You can use the built in fputcsv() for your arrays to generate correct csv lines from your array, so you will have to loop over and collect the lines. like:
$f = fopen("tmp.csv", "w");
foreach ($array as $line) {
fputcsv($f, $line)
}
要使浏览器提供另存为"对话框,您必须像这样发送HTTP标头(有关该标头的更多信息,请参见
To make the browsers offer the "Save as" dialog you will have to send HTTP headers like this (see more about this header in the rfc):
header('Content-Disposition: attachment; filename="filename.csv";');
将它们放在一起:
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
// open raw memory as file so no temp files needed, you might run out of memory though
$f = fopen('php://memory', 'w');
// loop over the input array
foreach ($array as $line) {
// generate csv lines from the inner arrays
fputcsv($f, $line, $delimiter);
}
// reset the file pointer to the start of the file
fseek($f, 0);
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachment; filename="'.$filename.'";');
// make php send the generated csv lines to the browser
fpassthru($f);
}
您可以像这样使用它:
array_to_csv_download(array(
array(1,2,3,4), // this array is going to be the first row
array(1,2,3,4)), // this array is going to be the second row
"numbers.csv"
);
更新:
除了php://memory
,您还可以使用 php://output
作为文件描述符,并取消查找等操作
Update:
Instead of the php://memory
you can also use the php://output
for the file descriptor and do away with the seeking and such:
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
// open the "output" stream
// see http://www.php.net/manual/en/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-descriptioq
$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
}