use*_*780 81 php export-to-csv
我是一名新手程序员,我搜索了很多关于我的问题,但找不到有用的解决方案或教程.
我的目标是我有一个PHP数组,数组元素显示在页面上的列表中.
我想添加一个选项,以便在用户需要时,他/她可以创建一个包含数组元素的CSV文件并下载它.
我不知道该怎么做.我也搜索了很多.但尚未找到任何有用的资源.
请为我提供一些教程,解决方案或建议,以便我自己实施.由于我是新手,请提供易于实施的解决方案.
我的数组看起来像:
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] =>
)
)
Run Code Online (Sandbox Code Playgroud)
com*_*857 181
您可以使用数组内置的fputcsv()从数组中生成正确的csv行,因此您必须循环并收集行.喜欢:
$f = fopen("tmp.csv", "w");
foreach ($array as $line) {
fputcsv($f, $line)
}
Run Code Online (Sandbox Code Playgroud)
要使浏览器提供"另存为"对话框,您必须像这样发送HTTP标头(请参阅rfc中有关此标头的更多信息):
header('Content-Disposition: attachment; filename="filename.csv";');
Run Code Online (Sandbox Code Playgroud)
把它们放在一起:
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);
}
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它:
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"
);
Run Code Online (Sandbox Code Playgroud)
更新:你可以使用for文件描述符
代替php://memory你php://output,并取消寻求和这样的:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Lux*_*ian 16
我没有足够的声誉来回复@ complex857解决方案.它工作得很好,但我不得不补充; 在Content-Disposition标头的末尾.没有它,浏览器会在文件名末尾添加两个破折号(例如,不是"export.csv",而是将文件保存为"export.csv--").可能它试图在标题行的末尾清理\ r \n.
正确的行应如下所示:
header('Content-Disposition: attachment;filename="'.$filename.'";');
Run Code Online (Sandbox Code Playgroud)
如果CSV中包含UTF-8字符,则必须通过更改Content-Type行将编码更改为UTF-8:
header('Content-Type: application/csv; charset=UTF-8');
Run Code Online (Sandbox Code Playgroud)
另外,我发现使用rewind()而不是fseek()更优雅:
rewind($f);
Run Code Online (Sandbox Code Playgroud)
谢谢你的解决方案!
Ind*_*ngh 11
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('dbname');
$qry = mysql_query("SELECT * FROM tablename");
$data = "";
while($row = mysql_fetch_array($qry)) {
$data .= $row['field1'].",".$row['field2'].",".$row['field3'].",".$row['field4']."\n";
}
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="filename.csv"');
echo $data; exit();
?>
Run Code Online (Sandbox Code Playgroud)
使用以下代码将php数组转换为CSV
<?php
$ROW=db_export_data();//Will return a php array
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=test.csv");
$fp = fopen('php://output', 'w');
foreach ($ROW as $row) {
fputcsv($fp, $row);
}
fclose($fp);
Run Code Online (Sandbox Code Playgroud)
那是我用于项目的功能,它可以按预期工作。
function array_csv_download( $array, $filename = "export.csv", $delimiter=";" )
{
header( 'Content-Type: application/csv' );
header( 'Content-Disposition: attachment; filename="' . $filename . '";' );
// clean output buffer
ob_end_clean();
$handle = fopen( 'php://output', 'w' );
// use keys as column titles
fputcsv( $handle, array_keys( $array['0'] ) );
foreach ( $array as $value ) {
fputcsv( $handle, $value , $delimiter );
}
fclose( $handle );
// flush buffer
ob_flush();
// use exit to get rid of unexpected output afterward
exit();
}
Run Code Online (Sandbox Code Playgroud)