栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过PHP导出为CSV

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过PHP导出为CSV

我个人使用此功能从任何数组创建CSV内容。

function array2csv(array &$array){   if (count($array) == 0) {     return null;   }   ob_start();   $df = fopen("php://output", 'w');   fputcsv($df, array_keys(reset($array)));   foreach ($array as $row) {      fputcsv($df, $row);   }   fclose($df);   return ob_get_clean();}

然后,您可以使用户使用以下方式下载该文件:

function download_send_headers($filename) {    // disable caching    $now = gmdate("D, d M Y H:i:s");    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");    header("Last-Modified: {$now} GMT");    // force download      header("Content-Type: application/force-download");    header("Content-Type: application/octet-stream");    header("Content-Type: application/download");    // disposition / encoding on response body    header("Content-Disposition: attachment;filename={$filename}");    header("Content-Transfer-Encoding: binary");}

用法示例:

download_send_headers("data_export_" . date("Y-m-d") . ".csv");echo array2csv($array);die();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/597046.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号