12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2022/8/10
- * Time: 18:39
- */
- namespace file;
- class DirHelper
- {
- /**
- * 创建多级目录
- * @param $path [目录路径]
- * @return bool
- */
- public static function makeDir($path)
- {
- if (!file_exists($path)) {//不存在则建立
- $mk = @mkdir($path, 0777, true); //权限
- @chmod($path, 0777);
- }
- return true;
- }
- /**
- * 不经过回收站,递归删除文件夹(本机测试时请慎用)
- * @param $dir [目录路径]
- */
- public static function delDir($dir)
- {
- if (file_exists($dir)) {
- $mydir = dir($dir);
- while (false !== ($file = $mydir->read())) {
- if ($file != "." && $file != "..") {
- $path = $dir . DS . $file;
- is_dir($path) ? self::delDir($path) : @unlink($path);
- }
- }
- $mydir->close();
- @rmdir($dir);
- }
- }
- /**
- * 复制目录
- * @param $source [源目录路径]
- * @param $dest [目标目录路径]
- * @param $cover [拷贝并覆盖同名文件]
- */
- public static function copyDir($source, $dest, $cover = true)
- {
- if (!file_exists($dest)) self::makeDir($dest);
- $handle = opendir($source);
- while (($item = readdir($handle)) !== false) {
- if ($item == '.' || $item == '..') continue;
- $_source = $source . DS . $item;
- $_dest = $dest . DS . $item;
- if (is_dir($_source)) {
- self::copyDir($_source, $_dest, $cover);
- } else if (is_file($_source)) {
- if ($cover || (!$cover && !file_exists($_dest))) {
- copy($_source, $_dest);
- }
- }
- }
- closedir($handle);
- }
- /**
- * 读取出一个文件夹及其子文件夹下所有文件
- * @param $path
- * @return array|mixed|string
- */
- public static function scanDirs($path)
- {
- global $result;
- $files = scandir($path);
- foreach ($files as $file) {
- if ($file != '.' && $file != '..') {
- if (is_dir($path . '/' . $file)) {
- $result[] = $path . '/' . $file;
- self::scanDirs($path . '/' . $file);
- } else {
- $result[] = $path . '/' . $file;
- }
- }
- }
- return $result;
- }
- }
|