如何从PHP数据库数据生成CSV文件?

更新于
2026-09-27 07:00:42
4阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计87个文字,预计阅读时间需要1分钟。

如何从PHP数据库数据生成CSV文件?

phpfunction generateCsv($data, $delimiter=',', $enclosure='') { $handle=fopen('php://temp', 'r+'); foreach ($data as $line) { fputcsv($handle, $line, $delimiter, $enclosure); } rewind($handle); while (!feof($handle)) { $contents .=fread($handle, 4096); } fclose($handle); return $contents;}

function generateCsv($data, $delimiter = ',', $enclosure = '"') { $handle = fopen('php://temp', 'r+'); foreach ($data as $line) { fputcsv($handle, $line, $delimiter, $enclosure); } rewind($handle); while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); return $contents; }
用法:

<?php $data[0] = "apple"; $data[1] = "oranges"; generateCsv($data, $delimiter = ',', $enclosure = '"'); ?>

如何从PHP数据库数据生成CSV文件?

本文共计87个文字,预计阅读时间需要1分钟。

如何从PHP数据库数据生成CSV文件?

phpfunction generateCsv($data, $delimiter=',', $enclosure='') { $handle=fopen('php://temp', 'r+'); foreach ($data as $line) { fputcsv($handle, $line, $delimiter, $enclosure); } rewind($handle); while (!feof($handle)) { $contents .=fread($handle, 4096); } fclose($handle); return $contents;}

function generateCsv($data, $delimiter = ',', $enclosure = '"') { $handle = fopen('php://temp', 'r+'); foreach ($data as $line) { fputcsv($handle, $line, $delimiter, $enclosure); } rewind($handle); while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); return $contents; }
用法:

<?php $data[0] = "apple"; $data[1] = "oranges"; generateCsv($data, $delimiter = ',', $enclosure = '"'); ?>

如何从PHP数据库数据生成CSV文件?