FileHelper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace lemo\helper;
  3. use RecursiveIteratorIterator;
  4. use RecursiveDirectoryIterator;
  5. class FileHelper
  6. {
  7. /**
  8. * 检测目录并循环创建目录
  9. *
  10. * @param $catalogue
  11. */
  12. public static function mkdirs($dir)
  13. {
  14. if (!file_exists($dir)) {
  15. self::mkdirs(dirname($dir));
  16. mkdir($dir, 0777);
  17. }
  18. return true;
  19. }
  20. /**
  21. * @param $dir
  22. * @return bool
  23. * 删除文件以及目录
  24. */
  25. public static function delDir($dir) {
  26. //先删除目录下的文件:
  27. // var_dump(is_dir($dir));
  28. // if(!is_dir($dir)){
  29. // return true;
  30. // }
  31. $dh=opendir($dir);
  32. while ($file=readdir($dh)) {
  33. if($file!="." && $file!="..") {
  34. $fullpath=$dir."/".$file;
  35. if(!is_dir($fullpath)) {
  36. unlink($fullpath);
  37. } else {
  38. self::delDir($fullpath);
  39. }
  40. }
  41. }
  42. closedir($dh);
  43. //删除当前文件夹:
  44. if(rmdir($dir)) {
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. /**
  51. * @param $source
  52. * @param $dest
  53. * 复制文件到指定文件
  54. */
  55. public static function copyDir($source, $dest)
  56. {
  57. if (!is_dir($dest)) {
  58. self::mkdirs($dest, 0755, true);
  59. }
  60. foreach (
  61. $iterator = new RecursiveIteratorIterator(
  62. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  63. RecursiveIteratorIterator::SELF_FIRST
  64. ) as $item
  65. ) {
  66. if ($item->isDir()) {
  67. $sontDir = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
  68. if (!is_dir($sontDir)) {
  69. self::mkdirs($sontDir, 0755, true);
  70. }
  71. } else {
  72. copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
  73. }
  74. }
  75. return true;
  76. }
  77. /*写入
  78. * @param string $type 1 为生成控制器 2 模型
  79. */
  80. public static function filePutContents($content,$filepath,$type){
  81. if($type==1){
  82. $str = file_get_contents($filepath);
  83. $parten = '/\s\/\*+start\*+\/(.*)\/\*+end\*+\//iUs';
  84. preg_match_all($parten,$str,$all);
  85. $ext_content = '';
  86. if($all[0]){
  87. foreach($all[0] as $key=>$val){
  88. $ext_content .= $val."\n\n";
  89. }
  90. }
  91. $content .= $ext_content."\n\n";
  92. $content .="}\n\n";
  93. }
  94. ob_start();
  95. echo $content;
  96. $_cache=ob_get_contents();
  97. ob_end_clean();
  98. if($_cache){
  99. $File = new \think\template\driver\File();
  100. $File->write($filepath, $_cache);
  101. }
  102. }
  103. /**
  104. * 获取文件夹大小
  105. *
  106. * @param string $dir 根文件夹路径
  107. * @return int
  108. */
  109. public static function getDirSize($dir)
  110. {
  111. if(!is_dir($dir)){
  112. return false;
  113. }
  114. $handle = opendir($dir);
  115. $sizeResult = 0;
  116. while (false !== ($FolderOrFile = readdir($handle))) {
  117. if ($FolderOrFile != "." && $FolderOrFile != "..") {
  118. if (is_dir("$dir/$FolderOrFile")) {
  119. $sizeResult += self::getDirSize("$dir/$FolderOrFile");
  120. } else {
  121. $sizeResult += filesize("$dir/$FolderOrFile");
  122. }
  123. }
  124. }
  125. closedir($handle);
  126. return $sizeResult;
  127. }
  128. /**
  129. * 创建文件
  130. *
  131. * @param $files
  132. */
  133. public static function createFile($file,$content)
  134. {
  135. $myfile = fopen($file, "w") or die("Unable to open file!");
  136. fwrite($myfile, $content);
  137. fclose($myfile);
  138. return true;
  139. }
  140. /**
  141. * 基于数组创建目录
  142. *
  143. * @param $files
  144. */
  145. public static function createDirOrFiles($files)
  146. {
  147. foreach ($files as $key => $value) {
  148. if (substr($value, -1) == '/') {
  149. mkdir($value);
  150. } else {
  151. file_put_contents($value, '');
  152. }
  153. }
  154. }
  155. // 判断文件或目录是否有写的权限
  156. public static function isWritable($file)
  157. {
  158. if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == FALSE) {
  159. return is_writable($file);
  160. }
  161. if (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) {
  162. return FALSE;
  163. }
  164. fclose($fp);
  165. return TRUE;
  166. }
  167. /**
  168. * 写入日志
  169. *
  170. * @param $path
  171. * @param $content
  172. * @return bool|int
  173. */
  174. public static function writeLog($path, $content)
  175. {
  176. self::mkdirs(dirname($path));
  177. return file_put_contents($path, "\r\n" . $content, FILE_APPEND);
  178. }
  179. }