PHP 导出csv,PHP导出csv 打包成zip

把数组处理成csv文件,并压缩成zip文件下载

zipfile('xxx.zip');

function zipfile($zipname) {
    
    $headers = ['1', '2', '3', '4'];

    $zip = new ZipArchive();
    $zip->open($zipname, ZipArchive::CREATE);
    
    // loop to create 3 csv files
    for ($i = 1; $i < 4; $i++) {
        $records = getRecord($i);
        $fd = putfile($zip, $headers, $records);
        // add the in-memory file to the archive, giving a name
        $zip->addFromString('file-'.$i.'.csv', stream_get_contents($fd));
        //close the file
        fclose($fd);
    }

    // close the archive
    $zip->close();

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$zipname);
    header('Content-Length: ' . filesize($zipname));
    readfile($zipname);

    // remove the zip archive
    // you could also use the temp file method above for this.
    unlink($zipname);

}

function getRecord($i) {
    $records1 = [
        ['111', '222', '333', '111'],
        ['111', '222', '333', '111'],
        ['111', '222', '333', '111'],
        ['111', '222', '333', '111'],
        ['111', '222', '333', '111'],
        ['111', '222', '333', '111'],
    ];

    $records2 = [
        ['111', '222', '333', '222'],
        ['111', '222', '333', '222'],
        ['111', '222', '333', '222'],
        ['111', '222', '333', '222'],
        ['111', '222', '333', '222'],
        ['111', '222', '333', '222'],
    ];

    $records3 = [
        ['111', '222', '333', '333'],
        ['111', '222', '333', '333'],
        ['111', '222', '333', '333'],
        ['111', '222', '333', '333'],
        ['111', '222', '333', '333'],
        ['111', '222', '333', '333'],
    ];
    $name = 'records' . $i;
    return $$name;
}

function putfile($zip, $headers, $records) {

    // create a temporary file
    $fd = fopen('php://temp/maxmemory:1048576', 'w');
    if (false === $fd) {
        die('Failed to create temporary file');
    }
    
    // write the data to csv
    fputcsv($fd, $headers);
    foreach($records as $record) {
        fputcsv($fd, $record);
    }

    // return to the start of the stream
    rewind($fd);

    return $fd;
}
直接输出csv文件 utf-8格式
function outputCsv($content) {

    $filename = 'xxx' . '_' . date('Ymd') . 'csv';
    header("Content-Disposition:attachment;filename=" . $filename);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    echo "\xEF\xBB\xBF"; // UTF-8 BOM

    echo $content;
整理一个包含csv头的数组导出成csv文件
public static function exportCsv($data)
    {
        if (count($data) === 0) {
            return '';
        }
        $file = fopen('php://memory', 'wb');
        $header = $data[0];
        $header_name = array_keys($header);
        $header_name_index = array_flip($header_name);
        $header_size = count($header);

        foreach ($data as $row) {
            $converted_row = [];
            for ($i = 0; $i < $header_size; $i++) {
                $converted_row[] = '';
            }
            foreach ($row as $key => $cell) {
                $index = -1;
                if (is_int($key) && $key >= 0 && $key < $header_size) {
                    $index = $key;
                } elseif (isset($header_name_index[$key])) {
                    $index = $header_name_index[$key];
                }
                if ($index >= 0) {
                    $converted_row[$index] = $cell;
                }
            }
            unset($key, $cell);
            ksort($converted_row, SORT_NUMERIC);
            fputcsv($file, $converted_row);
        }
        unset($row);

        fseek($file, 0, SEEK_SET);
        $content = '';
        $buffer_size = 8192;
        while (feof($file) === false) {
            $content .= fread($file, $buffer_size);
        }
        fclose($file);

        return $content;
    }

//输出csv文件,点击链接自动下载
outputCsv(self::exportCsv($data));

没法通过一次请求导出多个csv文件,只能通过zip打包多个csv文件,一次性下载。
大文件因为打开文件写入时间比较长,很容易timeout。
可以修改, 有一些作用。
nginx.conf

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

php-fpm

request_terminate_timeout = 60s
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 对于许多的从事数据智能开发的同僚来说,从库中提取出数据后进行数据整理并且导出csv文件的功能是很常见的,导出一个c...
    PeTu阅读 5,628评论 0 5
  • 一、MemCache简介 session MemCache是一个自由、源码开放、高性能、分布式的分布式内存对象缓存...
    李伟铭MIng阅读 9,300评论 2 13
  • [toc] 在公司的网站上推荐使用 docker 容器来安装环境,一个项目一个 docker 容器。 、、 百度百...
    Mdvtrw阅读 5,379评论 0 1
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 11,322评论 0 10
  • 第一世 现代 初次相遇 这座城市里繁华似锦,却没有人情味。时间一分一秒的过去,汽车川流不息的飞驰着,仿佛已...
    冬日里的暖暖阳光阅读 1,774评论 0 1

友情链接更多精彩内容