deletes($fullpath); } } } closedir($dh); //删除当前文件夹: if (rmdir($name)) { return true; } else { return false; } } /** * 把文件夹里面的文件打包成zip文件 * @param string $name 路径 * @param string $suffix_name 需要打包的后缀名,默认.png * @param string $file_name 文件名,默认全部名 * @return bool * @throws DtaException */ public function folderZip(string $name, string $suffix_name = '.png', string $file_name = '*'): bool { if (empty($name)) { throw new DtaException('请检查需要打包的路径名称'); } try { // 获取目录下所有某个结尾的文件列表 $list = glob($name . "{$file_name}.{$suffix_name}"); $fileList = $list; $zip = new ZipArchive(); // 打开压缩包 $zip->open($name, ZipArchive::CREATE); //向压缩包中添加文件 foreach ($fileList as $file) { $zip->addFile($file, basename($file)); } //关闭压缩包 $zip->close(); return true; } catch (\DtaException $e) { return false; } } /** * 获取目录下的所有文件和目录 * @param string $path * @return array */ public function getFiles(string $path): array { $files = []; if (is_dir($path)) { $path = dirname($path) . '/' . basename($path) . '/'; $file = dir($path); while (false !== ($entry = $file->read())) { if ($entry !== '.' && $entry !== '..') { $cur = $path . $entry; if (is_dir($cur)) { $subPath = $cur . '/'; $this->getFiles($subPath); } $files[] = $cur; } } $file->close(); return $files; } else { return []; } } /** * 删除目录下的文件 * @param string $path * @return bool */ public function rmFiles(string $path): bool { $files = $this->getFiles($path); if (!is_array($files)) { return false; } elseif (empty($files)) { return false; } else { foreach ($files as $item => $file) { if (is_dir($file)) { rmdir($file); } elseif (is_file($file)) { unlink($file); } } } return true; } /** * 判断文件是否存在 * @param string $path * @return bool */ public function judgeFile(string $path): bool { if (file_exists($path)) { return true; } return false; } /** * 判断目录是否存在 * @param string $path * @return bool */ public function judgeContents(string $path): bool { if (is_dir($path)) { return true; } return false; } }