当前位置:网站首页>关于CI框架批量导出至压缩文件
关于CI框架批量导出至压缩文件
2022-06-11 09:20:00 【cathy1213】
最近收到了一个需求,需要将很多文件导出为压缩文件,刚开始以为需要先一层一层将目录创建好,把对应文件放进去,然后利用压缩类对大文件夹整体压缩,后面发现没这么复杂,CI的read_file方法直接将文件读取进指定位置就可以了,然后download下载之后就是你想要的层级结构。
实践之后遇到了一个问题,阿里云oss的文件没法直接写入到压缩文件,可能是读取不到吧,因为本地文件是可以直接写入的。在网上没找到解决方法,想了一下,干脆将文件先写入本地,然后从本地写入压缩文件吧,问题又来了,常用的file_get_contents和file_put_contents没法将oss文件写入到本地,可能我技术不精,换个姿势使用curl读取,成功了,问题解决。下面是实现方式。。。没有去深究,如果有大神帮我解答原因,十分感激。
$cur_encoding = iconv_get_encoding(); //获取当前iconv的编码,方便后面转换
$this->load->library('zip');
foreach ($attach_file as $v){
$extend2 = strtolower(strrchr($v['url'], '.')); //获取后缀
//---------使用curl将文件写入本地
$ch = curl_init($v['url']);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata = curl_exec ($ch);
curl_close ($ch);
$fp = fopen(APPPATH.'cache/tmpdocx/a'.$extend2,'w'); //fopen的第一个参数,如果指定目录的话,目录如果不存在,将不会写入成功。也可以直接放在根目录下,反正这个文件只是一个临时文件,在写入压缩文件后还是会删除的。
fwrite($fp, $rawdata);
fclose($fp);
//----------写入完毕。
$dir_name = $dir_title.'/任务书/';
$dir_name = iconv ( $cur_encoding['input_encoding'], 'GBK', $dir_name); //这里是确保文件名编码支持中文,用iconv转为GBK
$file_name = iconv($cur_encoding['input_encoding'],'GBK',$v['name']);
//用read_file写入压缩问价,第一个参数就是前面利用curl写入的本地文件,第二个参数是目标位置,支持层级,也就是压缩文件里面对应的目录层级。
$this->zip->read_file(APPPATH.'cache/tmpdocx/a'.$extend2,$dir_name.$file_name);
//删除curl生成的临时文件。
unlink(APPPATH.'cache/tmpdocx/a'.$extend2);
}
//所有文件写入压缩文件之后,导出,浏览器会自动下载。
$this->zip->download('资料.zip');
边栏推荐
- Flask (IV) -- URL construction
- LeetCode刷题 —— 手撕二叉树
- Fabric. JS dynamically set font size
- Telecommuting best practices and Strategies
- ORACLE RAC中连接ScanIP报错ORA-12545的问题解决
- POJ3250「Bad Hair Day」
- About prototype and prototype chain
- Machine learning notes - spatial transformer network using tensorflow
- [TiO websocket] III. The TiO websocket server can send messages to users anywhere
- 「INS-30131」 安装程序验证所需的初始设置失败
猜你喜欢

考研数学 【数列极限证明题】题型方法总结

Day45 storage engine data type integer floating point character type date type enumeration and set type constraints table to table relationships

RAC expdp export error: ora-31693, ora-31617, ora-19505

ArcGIS 10.9.1 geological and meteorological volume metadata processing and service publishing and calling

Augmented reality experiment IV of Shandong University

RAC单独修改scanip到不同网段时会报错

Type-C Bluetooth speaker single port rechargeable OTG solution

Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)

Flask (VIII) - form processing

「INS-30131」 安装程序验证所需的初始设置失败
随机推荐
Machine learning notes - convolutional neural network memo list
Opencv image basic operation (IV) -- image feature extraction (corner detection)
Fabric.js 動態設置字號大小
Day39 content summary
Opencv image basic operation (III) -- image feature extraction (corner detection)
The mobile terminal page uses REM for adaptation
Remote office related issues to be considered by enterprises
Version mismatch between installed deeply lib and the required one by the script
How do we connect to WiFi?
Day44 database
The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)
赛灵思引脚约束文件 .xdc
ORACLE DG物理备库使用别名数据文件改变路径到OMF路径
报错Version mismatch between installed depthai lib and the required one by the scrip.
等待事件 enq: KO - fast object checkpoint可行的一些处理方法
Device = depthai Device(““, False) TypeError: _init_(): incompatible constructor arguments.
Oracle XDB組件的重建
A summary of the problem type and method for proving the limit of sequence in postgraduate entrance examination
OpenCV OAK相机对比及介绍
Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them