1.将文件夹或者文件打入压缩包
$zip = new ZipArchive();
if ($zip->open('D:/php/test.zip') === true) {
// 将D:/php/newDir目录下的文件或者文件夹打入压缩包D:/php/test.zip下面的testOne目录下
addFileToZip('D:/php/newDir', $zip,'testOne');
$zip->close();
}else{
echo '打开路径文件:'.'D:/php/test.zip'.'失败'. "\n";
}
/**
* 将文件夹打入zip压缩包里面
* param $folder 要打入的文件夹或者文件路径
* param $zipFile 打开的zip文件
* param $subfolder zip子目录(设置了就打入子目录下面)
* return boolean
*/
public static function addFileToZip($folder, $zipFile, $subfolder = null)
{
if ($zipFile == null) {
return false;
}
$str = str_split($folder);
$end = end($str) == '/' ? '' : '/';
$folder .= $end;
$subStr = str_split($subfolder);
$subEnd = end($subStr) == '/' || $subfolder == NULL ? '' : '/';
$subfolder .= $subEnd;
$handle = opendir($folder);
while ($f = readdir($handle)) {
if ($f != '.' && $f != '..' && $f != 'archive') {
if (is_file($folder . $f)) {
$zipFile->addFile($folder . $f, $subfolder . $f);
} elseif (is_dir($folder . $f)) {
$zipFile->addEmptyDir($subfolder . $f);
self::addFileToZip($folder . $f, $zipFile, $subfolder . $f);
}
}
}
closedir($handle);
return true;
}
2.删除文件夹
deldir('d:/app')
public static function deldir($dir) {
//先删除目录下的文件:
$dh = opendir($dir);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
$fullpath = $dir . "/" . $file;
if (!is_dir($fullpath)) {
unlink($fullpath);
} else {
self::deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if (rmdir($dir)) {
return true;
} else {
return false;
}
}