php获取压缩文件列表,php判断zip压缩包内文件目录结构

该博客讲述了如何检查ZIP压缩包中的目录层级,如果存在多次文件夹则发出警告。使用了File_Archive库来读取目录结构,并通过自定义方法删除现有目录、解压文件以及检查是否有子目录。整个过程涉及文件操作、权限设置和错误日志记录。
摘要由CSDN通过智能技术生成

判断一个压缩包中的目录层次,如果有多次文件夹就警告错误。

这里用到了三个方法,即:读取Zip目录结构、删除原有或指定的文件夹(目录)、判断指定目录是不是有子目录类。

主要用到的是require_once "File/Archive.php"; 另外就是自己写的后两个方法。

define("ZHOZ_TMP_DIR", "home/zhoz/temp_upload");

$file_name = "./zhozcom.zip";

require_once "File/Archive.php";

$source = File_Archive::read($file_name);

// 读取目录结构

$directory_name_list = array();

while($source->next()) {

if ((preg_match_all("/^([\w\-\.]+)\/([\w\-\.]+)$/", $source->getFilename(), $tmp))) {

if (!in_array($tmp[1], $directory_name_list)) {

array_push($directory_name_list, $tmp[1]);

}

}

}

if (count($directory_name_list) > 0) {

// 以前存在的目录删除

foreach ($directory_name_list as $tmp) {

if (is_dir(ZHOZ_TMP_DIR . "/" . $tmp[0])) {

ZhozHandler::delFileDir(ZHOZ_TMP_DIR . "/" . $tmp[0]);

error_log($tmp[0] . "

}

}

// 解压文件

$command = "unzip -o " . UPLOAD_TMP_DIR . "/". $file_name . " -d " . ZHOZ_TMP_DIR;

error_log($command, 0);

exec($command);

foreach ($directory_name_list as $tmp_dir) {

// 存在子目录

$zip_dir = ZhozHandler::checkNextDir(ZHOZ_TMP_DIR . "/". $tmp_dir[0]);

error_log($tmp_dir[0] . "

if ($zip_dir) {

$check_flg = true;

error_log($tmp_dir[0] . "

}

}

} else {

// 没找到目录也返回

$check_flg = true;

error_log("countdir----------is-----null", 0);

}

$command = "chmod -R 777 " . ZHOZ_TMP_DIR;

error_log($command, 0);

exec($command);

// ファイル削除

if (is_file(UPLOAD_TMP_DIR . "/". $file_name)) {

unlink(UPLOAD_TMP_DIR . "/". $file_name);

}

PHP删除指定的文件夹

下面这两个方法,将包含在ZhozHandler类中:

function delFileDir($dir) {

$dh=opendir($dir);

while ($file=readdir($dh)) {

if($file!="." && $file!="..") {

$fullpath = $dir."/".$file;

if(!is_dir($fullpath)) {

unlink($fullpath);

} else {

ZhozHandler::delFileDir($fullpath);

}

}

}

closedir($dh);

if (rmdir($dir)) {

return true;

} else {

return false;

}

}

判断是不是有子目录的方法:

function checkNextDir($directory) {

if(!is_dir($directory)) {

return true;

}

$handle = opendir($directory);

while (($file = readdir($handle)) !== false) {

if ($file != "." && $file != "..") {

if(is_dir($directory.'/'.$file)) {

return true;

}

}

}

closedir($handle);

return false;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值