zl程序教程

您现在的位置是:首页 >  其他

当前栏目

php案例:压缩多个文件

案例PHP文件 多个 压缩
2023-06-13 09:13:56 时间

作者:陈业贵 华为云享专家 51cto(专家博主 明日之星 TOP红人) 阿里云专家博主

文章目录


前言

学习学习怎么压缩多个文件.

1.php

<?php
$file=dirname(__FILE__);
	//定位绝对路径到这里D:\phpstudy_pro\WWW
	$file1=str_replace('\\', '/',$file);//然后把\改成/
	
	$path=$file1."/66.txt";//后面在添加66.txt文件变成D:/phpstudy_pro/WWW/66.txt
	$path1=$file1."/666.txt";//后面在添加666.txt文件变成D:/phpstudy_pro/WWW/666.txt
$fileList =[$path,$path1];

$filename = "test.zip";//把66.txt+666.txt文件都压缩进test.zip压缩包中

$zip = new ZipArchive();//创建压缩包镀锡

$zip->open($filename,ZipArchive::CREATE); //打开压缩包

foreach($fileList as $file){//遍历两个文件66.txt 666.txt

$zip->addFile($file,basename($file)); //向压缩包中添加文件(66.txt 666.txt)

}

$zip->close(); //关闭压缩包

效果