实例代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public function downLoad( $dataUrl , $saveName )
{
$datalist = [
ROOT_PATH. '/public/introduce/110.docx' ,
ROOT_PATH. '/public/upfile/110.zip'
];
// print_r($datalist);die;
$filename = ROOT_PATH. '\\public\\/' . $saveName . '.zip' ;
if ( file_exists ( $filename )){
unlink( $filename );
}
$zip = new \\ZipArchive();
if ( $zip ->open( $filename ,\\ZipArchive::CREATE)!== true){
exit ( '无法打开文件,或者文件创建失败' );
}
foreach ( $dataUrl as $index => $item ) {
if (DIRECTORY_SEPARATOR== '\\\\' ){
$item = str_replace ( '/' ,DIRECTORY_SEPARATOR, $item );
$filename = str_replace ( '/' ,DIRECTORY_SEPARATOR, $filename );
}
// var_dump($item);
// var_dump(file_exists($item));die;
if ( file_exists ( $item )){
$zip ->addFile( $item , basename ( $item ));
}
}
$zip ->close();
if (! file_exists ( $filename )){
exit ( "无法找到文件" ); //即使创建,仍有可能失败
}
header( 'Content-Type: application/zip' );
header( 'Content-disposition: attachment; filename=' . basename ( $filename ));
header( 'Content-Length: ' . filesize ( $filename ));
@readfile( $filename );
@unlink ( $filename );
}
|
注意:里面的路径全部用绝对路径,不然会找不到文件
附赠其他操作:
解压缩zip文件
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public function unzip_file( $file , $dir ){
// 实例化对象
$zip = new ZipArchive() ;
//打开zip文档,如果打开失败返回提示信息
if ( $zip ->open( $file ) !== TRUE) {
die ( "Could not open archive" );
}
//将压缩文件解压到指定的目录下
$zip ->extractTo( $dir );
//关闭zip文档
$zip ->close();
}
|
获取解压文件目录
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public function loopFun( $dir )
{
$handle = opendir( $dir . "." );
//定义用于存储文件名的数组
$array_file = array ();
while (false !== ( $file = readdir( $handle )))
{
if ( $file != "." && $file != ".." ) {
$array_file [] = $dir . '/' . $file ; //输出文件名
}
}
closedir ( $handle );
return $array_file ;
//print_r($array_file);
}
|
大家可以在本地测试下,感谢大家的学习和对快网idc的支持。
原文链接:https://www.cnblogs.com/zhangdong156/p/11771174.html
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 50
-
2025-05-25 61
-
2025-05-27 30
-
linux 打造man中文手册图解(man-pages-zh帮助页)
2025-05-27 75 -
2025-05-25 98
热门评论