DirFile.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace laytp\library;
  3. use DateTime;
  4. use DateTimeZone;
  5. use RecursiveDirectoryIterator;
  6. use RecursiveIteratorIterator;
  7. use think\facade\Config;
  8. /**
  9. * 文件夹和文件处理类
  10. */
  11. class DirFile
  12. {
  13. /**
  14. * 创建目录
  15. * @param $path
  16. * @param int $mode
  17. * @return bool
  18. */
  19. public static function createDir($path, $mode = 0777)
  20. {
  21. if (is_dir($path)) {
  22. return true;
  23. } else {
  24. //如果目录不存在,则递归创建
  25. if (mkdir($path, $mode, true)) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. }
  31. }
  32. /**
  33. * 循环遍历目录下的文件和文件夹并输出
  34. * @param $pathName
  35. * @param array $filterDir
  36. * @param array $filterFile 需要过滤的文件名,*表示所有文件不展示
  37. * @param array $selected 设置是否已经选中
  38. * @param array $output
  39. * @return array|null
  40. */
  41. public static function recurDir($pathName, $filterDir = [], $filterFile = [], $selected = [], $output = ['id'=>'id', 'fullName' => 'fullName', 'baseName' => 'baseName'])
  42. {
  43. //将结果保存在result变量中
  44. $result = [];
  45. $temp = [];
  46. //判断传入的变量是否是目录
  47. if (!is_dir($pathName) || !is_readable($pathName)) {
  48. return null;
  49. }
  50. //取出目录中的文件和子目录名,使用scandir函数
  51. $allFiles = scandir($pathName);
  52. //遍历他们
  53. foreach ($allFiles as $fileName) {
  54. if (in_array($fileName, ['.', '..'])) {
  55. continue;
  56. }
  57. //路径加文件名
  58. if (mb_substr($pathName, -1, 1) == DS) {
  59. $fullName = $pathName . $fileName;
  60. } else {
  61. $fullName = $pathName . DS . $fileName;
  62. }
  63. $baseName = basename($fileName);
  64. //如果是目录的话就继续遍历这个目录
  65. if (is_dir($fullName)) {
  66. if (in_array($fullName, $filterDir)) {
  67. continue;
  68. }
  69. //将这个目录中的文件信息存入到数组中
  70. $res = [
  71. $output['baseName'] => $baseName,
  72. $output['fullName'] => $fullName,
  73. 'type' => 'dir',
  74. 'children' => self::recurDir($fullName, $filterDir, $filterFile, $selected, $output),
  75. ];
  76. if (in_array($fullName, $selected)) {
  77. $res['state'] = ['selected' => true];
  78. } else {
  79. $res['state'] = ['selected' => false];
  80. }
  81. $apiDirName = Config::get('apidirname');
  82. if(isset($apiDirName[$fullName])){
  83. $res['name'] = $apiDirName[$fullName];
  84. }else{
  85. $res['name'] = '';
  86. }
  87. $res['id'] = $fullName;
  88. $result[] = $res;
  89. } else {
  90. if($filterFile != '*'){
  91. if (in_array($fullName, $filterFile)) {
  92. continue;
  93. }
  94. //如果是文件就先存入临时变量
  95. //将这个目录中的文件信息存入到数组中
  96. $tem = [$output['id'] => $fullName, $output['baseName'] => $baseName, $output['fullName'] => $fullName, 'type' => 'file', 'state' => ['selected' => false]];
  97. if (in_array($fullName, $selected)) {
  98. $tem['state'] = ['selected' => true];
  99. } else {
  100. $tem['state'] = ['selected' => false];
  101. }
  102. $temp[] = $tem;
  103. }
  104. }
  105. }
  106. //取出文件
  107. if ($temp) {
  108. foreach ($temp as $f) {
  109. $result[] = $f;
  110. }
  111. }
  112. return $result;
  113. }
  114. /**
  115. * 删除文件夹
  116. * @param string $dirname 目录
  117. * @param bool $withself 是否删除自身
  118. * @return boolean
  119. */
  120. public static function rmDirs($dirname, $withself = true)
  121. {
  122. if (!is_dir($dirname)) {
  123. return false;
  124. }
  125. $files = new RecursiveIteratorIterator(
  126. new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
  127. RecursiveIteratorIterator::CHILD_FIRST
  128. );
  129. foreach ($files as $fileinfo) {
  130. $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
  131. $todo($fileinfo->getRealPath());
  132. }
  133. if ($withself) {
  134. @rmdir($dirname);
  135. }
  136. return true;
  137. }
  138. /**
  139. * 复制文件夹
  140. * @param string $source 源文件夹
  141. * @param string $dest 目标文件夹
  142. */
  143. public static function copyDirs($source, $dest)
  144. {
  145. if (!is_dir($dest)) {
  146. mkdir($dest, 0755, true);
  147. }
  148. foreach (
  149. $iterator = new RecursiveIteratorIterator(
  150. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  151. RecursiveIteratorIterator::SELF_FIRST
  152. ) as $item
  153. ) {
  154. if ($item->isDir()) {
  155. $sontDir = $dest . DS . $iterator->getSubPathName();
  156. if (!is_dir($sontDir)) {
  157. mkdir($sontDir, 0755, true);
  158. }
  159. } else {
  160. copy($item, $dest . DS . $iterator->getSubPathName());
  161. }
  162. }
  163. }
  164. }