redis中,我的数据格式是json,如下:
[{"uniquekey":{"name":"uniquekey","value":"09a52159eb4c52f49035088e8ca85dd6","handle_tyle":"","sort":0,"is_search":"","_id":"0","is_checked":"","alias":"uniquekey"},"title":{"name":"title","value":"\u201c\u82af\u7247\u4e00\u54e5\u201d\u8bfa\u5b89\u8521\u5d69\u677e\u8c03\u4ed3\uff01\u9884\u5224\u534a\u5bfc\u4f53\u5ef6\u7eed\u7f3a\u8d27\u6f6e\uff0c\u653e\u8a00\u201c\u9ece\u660e\u524d\u7684\u9ed1\u6697\uff0c\u4e00\u89e6\u5373\u53d1\u201d","handle_tyle":"","sort":0,"is_search":"","_id":"1","is_checked":"","alias":"title"},"date":{"name":"date","value":"2021-04-23 17:24:00","handle_tyle":"","sort":0,"is_search":"","_id":"2","is_checked":"","alias":"date"},"category":{"name":"category","value":"\u5934\u6761","handle_tyle":"","sort":0,"is_search":"","_id":"3","is_checked":"","alias":"category"}},{"uniquekey":{"name":"uniquekey","value":"09a52159eb4c52f49035088e8ca85dd6","handle_tyle":"","sort":0,"is_search":"","_id":"0","is_checked":"","alias":"uniquekey"},"title":{"name":"title","value":"\u201c\u82af\u7247\u4e00\u54e5\u201d\u8bfa\u5b89\u8521\u5d69\u677e\u8c03\u4ed3\uff01\u9884\u5224\u534a\u5bfc\u4f53\u5ef6\u7eed\u7f3a\u8d27\u6f6e\uff0c\u653e\u8a00\u201c\u9ece\u660e\u524d\u7684\u9ed1\u6697\uff0c\u4e00\u89e6\u5373\u53d1\u201d","handle_tyle":"","sort":0,"is_search":"","_id":"1","is_checked":"","alias":"title"},"date":{"name":"date","value":"2021-04-23 17:24:00","handle_tyle":"","sort":0,"is_search":"","_id":"2","is_checked":"","alias":"date"},"category":{"name":"category","value":"\u5934\u6761","handle_tyle":"","sort":0,"is_search":"","_id":"3","is_checked":"","alias":"category"}}]
连接redis:
$class=new \Redis();
$class->connect('localhost', 6379);//地址和端口
$class->auth('123456'); //填写密码
$class->select(2); 选择redis的第三个库
不存在文件就创建文件,并把读取出来的数据写入csv文件中
$path = $_SERVER['DOCUMENT_ROOT']."/csv/".date("Y-m-d",time())."/";
if (!is_dir($path)){ //判断目录是否存在 不存在就创建
mkdir($path,0777,true);
}
$data=[];
//如果源地址有文件就清空.永远保持目录下只有三个文件
$sspath=$_SERVER['DOCUMENT_ROOT']."/csv/".date("Y-m-d",time())."/";
foreach(scandir($sspath) as $val){
if($val !="." && $val !=".."){
if(is_dir($sspath.$val)){
@rmdir($sspath.$val.'/');
}else{
//如果是文件直接删除
unlink($sspath.$val);
}
}
}
foreach ($codes as $code){
$data[$code]=$class->hGet($code,'convert');
$data[$code]=json_decode($data[$code],true);
$res=[];
if (is_array($data[$code])){
foreach ($data[$code] as $k=> &$v){
$value='';
if (is_iterable($v)){
foreach ($v as $kk=>&$vv){
if (!isset($vv['value'])){
unset($data[$code][$k]);
}else{
$value=$vv['value'];
$res[$kk]=$value;
}
}
$v=$res;
}
}
$data[$code]['ree']=[];
$data[$code]['title']=[];
foreach ($data[$code] as $k=>$v){
foreach ($v as $kk=>$vv){
$data[$code]['ree'][$k][]=$vv;
$data[$code]['title'][]=$kk;
}
}
$data[$code]['title']=array_unique($data[$code]['title']);
array_unshift($data[$code]['ree'],$data[$code]['title']);
$filename = $path.$code.'.csv'; //设置文件名
$fp= fopen($filename, 'w');
// 对于用 wps 和编辑器打开无乱码但是用 excel 打开出现乱码的问题,可以添加以下一行代码解决问题
fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
foreach ($data[$code]['ree'] as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
}
压缩成zip文件,最后打包生成以时间戳为准的zip
$zip=new \ZipArchive();
$zipname = time().".zip";
if (!is_file($sspath.$zipname)){
file_put_contents($sspath.$zipname,'');
}
$zip->open($sspath.$zipname, \ZipArchive::OVERWRITE);
$files=[];
$srcpath=$path;
if (is_dir($path)){
$path=scandir($path);
foreach ($path as $k=>$c){
if ($c=='.'||($c=='..')||strtolower(pathinfo($c)['extension'])=='zip'){
unset($path[$k]);
}else{
if (strtolower(pathinfo($c)['extension'])=='csv'){
$files[]=$c;
}
}
}
}
if (empty($files)){
return $this->asErrorStr(-12504629);
}
foreach ($files as $file) {
$zip->addFile($srcpath.$file,$file);
}
$zip->close();
至此,zip文件已经生成完成,但是如果想要在客户端下载的话,还需要添加如下代码
$fname=$sspath.$zipname;
Header("Content-Type: application/zip");
Header("Content-Transfer-Encoding: binary");
Header("Accept-Ranges: bytes");
Header("Content-Disposition: attachment; filename=".$zipname);
echo readfile($fname);