FileHelper.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace common\helpers;
  3. use yii\helpers\BaseFileHelper;
  4. /**
  5. * Class FileHelper
  6. * @package common\helpers
  7. */
  8. class FileHelper extends BaseFileHelper
  9. {
  10. /**
  11. * 检测目录并循环创建目录
  12. *
  13. * @param $catalogue
  14. * @return bool
  15. */
  16. public static function mkdirs($catalogue)
  17. {
  18. if (!file_exists($catalogue)) {
  19. self::mkdirs(dirname($catalogue));
  20. mkdir($catalogue, 0777);
  21. }
  22. return true;
  23. }
  24. /**
  25. * 写入日志
  26. *
  27. * @param $path
  28. * @param $content
  29. * @return bool|int
  30. */
  31. public static function writeLog($path, $content)
  32. {
  33. self::mkdirs(dirname($path));
  34. $content = '[' . date('Y-m-d H:i:s') . '] ' . $content;
  35. return file_put_contents($path, "\r\n" . $content, FILE_APPEND);
  36. }
  37. /**
  38. * 获取文件夹大小
  39. *
  40. * @param string $dir 根文件夹路径
  41. * @return int
  42. */
  43. public static function getDirSize($dir)
  44. {
  45. $handle = opendir($dir);
  46. $sizeResult = 0;
  47. while (false !== ($folderOrFile = readdir($handle))) {
  48. if ($folderOrFile != "." && $folderOrFile != "..") {
  49. if (is_dir("$dir/$folderOrFile")) {
  50. $sizeResult += self::getDirSize("$dir/$folderOrFile");
  51. } else {
  52. $sizeResult += filesize("$dir/$folderOrFile");
  53. }
  54. }
  55. }
  56. closedir($handle);
  57. return $sizeResult;
  58. }
  59. /**
  60. * 基于数组创建目录
  61. *
  62. * @param $files
  63. */
  64. public static function createDirOrFiles($files)
  65. {
  66. foreach ($files as $key => $value) {
  67. if (substr($value, -1) == '/') {
  68. mkdir($value);
  69. } else {
  70. file_put_contents($value, '');
  71. }
  72. }
  73. }
  74. /**
  75. * 软著文件生成
  76. *
  77. * @param $dir
  78. * @param $savePath
  79. */
  80. public static function getDirFileContent($dir, $savePath, $suffix = ['php'])
  81. {
  82. $handle = opendir($dir);
  83. while (false !== ($folderOrFile = readdir($handle))) {
  84. if ($folderOrFile != "." && $folderOrFile != "..") {
  85. if (is_dir("$dir/$folderOrFile")) {
  86. self::getDirFileContent("$dir/$folderOrFile", $savePath, $suffix);
  87. } else {
  88. $array = explode('.', $folderOrFile);
  89. if (in_array(end($array), $suffix)) {
  90. // 去除注释
  91. $str = StringHelper::removeAnnotation(file_get_contents("$dir/$folderOrFile"));
  92. // 追加写入
  93. file_put_contents($savePath, $str, FILE_APPEND);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * @name:
  101. * @msg: 把一个或者多个文件打包压缩为一个文件
  102. * @param {array} $files 需要打包压缩的文件
  103. * @param {string} $zip_filename 生成的压缩包文件名称
  104. * @return string 压缩成功返回生成的压缩文件的路径,否则返回 false
  105. */
  106. public static function packFiles(array $files = [], string $zip_filename = '')
  107. {
  108. try {
  109. if (empty($files)) return false;
  110. $path = \Yii::getAlias('@attachment') . '/download/zips';
  111. if (!file_exists($path)) {
  112. mkdir($path, 0755, true);
  113. }
  114. $temp_path = \Yii::getAlias('@attachment') . '/download/temp';
  115. if (!file_exists($temp_path)) {
  116. mkdir($temp_path, 0755, true);
  117. }
  118. $zip = new \ZipArchive();
  119. if (empty($zip_filename)) {
  120. $zip_filename = $path . '/' . '压缩文件-' . date('YmdHis') . mt_rand(0, 100) . '.zip';
  121. } else {
  122. $zip_filename = $path . '/' . $zip_filename . '-' . date('YmdHis') . mt_rand(0, 100) . '.zip';
  123. }
  124. $zip->open($zip_filename, \ZipArchive::CREATE);
  125. foreach ($files as $file) {
  126. $csv_filename = $temp_path . '/' . pathinfo($file, PATHINFO_BASENAME);
  127. if (!file_exists($csv_filename)) {
  128. $csv_str = file_get_contents($file);
  129. file_put_contents($csv_filename, $csv_str);
  130. }
  131. $zip->addFile($csv_filename, basename($csv_filename));
  132. }
  133. $zip->close();
  134. return $zip_filename;
  135. } catch (\Exception $e) {
  136. \Yii::error('打包压缩文件失败,errmsg:' . $e->getMessage());
  137. \Yii::getLogger()->flush(true);
  138. return false;
  139. }
  140. }
  141. public static function downFile($save_path,$file_name,$file_url){
  142. $path_info_arr = pathinfo($file_url);
  143. if(!is_dir($save_path)) mkdir($save_path,0777,true);
  144. $file = $save_path.'/'.$file_name.'.'.$path_info_arr['extension'];
  145. $file_resource = file_get_contents($file_url);
  146. $res = file_put_contents($file,$file_resource);
  147. if($res) return $file;
  148. return '';
  149. }
  150. public static function recursiveDelete($directory){
  151. if (!$handler = opendir($directory)) {
  152. return false;
  153. }
  154. while (($file = readdir($handler)) !== false) {
  155. if ($file !== '.' && $file !== '..') {
  156. $file = $directory . '/' . $file;
  157. if (is_dir($file)) {
  158. self::recursiveDelete($file);
  159. } else {
  160. @unlink($file);
  161. }
  162. }
  163. }
  164. @rmdir($directory);
  165. }
  166. }