Skip to content

Instantly share code, notes, and snippets.

@shouhei
Created April 22, 2014 23:38
Show Gist options
  • Save shouhei/11198068 to your computer and use it in GitHub Desktop.
Save shouhei/11198068 to your computer and use it in GitHub Desktop.
php -S localhost:8888 sample.php とかやると、http://localhost:8888/sample.phpがcsv形式のファイルを生成して、ダウンロードさせるようなページになります。
<?php
exec("rm *.csv");
exec("rm *.zip");
$datas = array();
$datas[] = "hoge,hoge,hoge,hoge\nhoge,hoge,hoge";
$datas[] = "fizz,buzz,fizzbuzz\nfizz,buzz,fizzbuzz";
$csv_list = array();
foreach($datas as $key => $data){
$name = $key . "-". date(YmdHis) .".". "csv";
if(!file_exists($name)){
$file = fopen($name,'a');
if(!fwrite($file, $data)){
echo "error";
}else{
$csv_list[] = $name;
}
}
}
$zip = new ZipArchive();
$res = $zip->open('result.zip', ZipArchive::CREATE);
if ($res === true) {
// 圧縮するファイルを指定する
foreach($csv_list as $csv){
$zip->addFile($csv);
}
// ZIPファイルをクローズ
$zip->close();
}
header('Content-Type: application/force-download');
header('Content-Length: '.filesize("result.zip"));
header('Content-disposition: attachment; filename=result.zip');
readfile("result.zip");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment