@@ -4,8 +4,8 @@ discard block |
||
| 4 | 4 | * 文件操作类 |
| 5 | 5 | * logFilePath: /storage/tinymeng/log/ |
| 6 | 6 | */ |
| 7 | -define('logFilePath',dirname(dirname(dirname(dirname(__DIR__)))).DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'tinymeng'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR); |
|
| 8 | -class File{ |
|
| 7 | +define('logFilePath', dirname(dirname(dirname(dirname(__DIR__)))).DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'tinymeng'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR); |
|
| 8 | +class File { |
|
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Name: writeLog |
@@ -16,26 +16,26 @@ discard block |
||
| 16 | 16 | * @param \Exception|null $exception |
| 17 | 17 | * @return bool |
| 18 | 18 | */ |
| 19 | - static public function writeLog($message, $file_name='error',bool $echo = false,\Exception $exception = null){ |
|
| 20 | - if(!is_string($message)){ |
|
| 19 | + static public function writeLog($message, $file_name = 'error', bool $echo = false, \Exception $exception = null) { |
|
| 20 | + if (!is_string($message)) { |
|
| 21 | 21 | $message = json_encode($message); |
| 22 | 22 | } |
| 23 | 23 | $message = date('Y-m-d H:i:s').' : '.$message.PHP_EOL; |
| 24 | - if($exception && $exception instanceof \Exception){ |
|
| 24 | + if ($exception && $exception instanceof \Exception) { |
|
| 25 | 25 | $message .= ' File: '.$exception->getFile().' ,Line: '.$exception->getLine().' ,Message: '.$exception->getMessage(); |
| 26 | 26 | } |
| 27 | - if($echo){ |
|
| 27 | + if ($echo) { |
|
| 28 | 28 | echo $message; |
| 29 | 29 | } |
| 30 | 30 | $path = logFilePath; |
| 31 | 31 | if (!is_dir($path)) { |
| 32 | - if(!mkdir($path, 0755, true)){ |
|
| 32 | + if (!mkdir($path, 0755, true)) { |
|
| 33 | 33 | die('创建缓存文件夹"'.$path.'"失败!'); |
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | $file_name = $path.$file_name; |
| 38 | - self::filePutContents($file_name."-".date('Ymd',time()).".log",$message,true); |
|
| 38 | + self::filePutContents($file_name."-".date('Ymd', time()).".log", $message, true); |
|
| 39 | 39 | return true; |
| 40 | 40 | } |
| 41 | 41 | |
@@ -48,10 +48,10 @@ discard block |
||
| 48 | 48 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
| 49 | 49 | * http://blog.majiameng.com/article/2724.html |
| 50 | 50 | */ |
| 51 | - static public function filePutContents(string $file_name,string $content,bool $file_append = false){ |
|
| 52 | - if(strrpos($file_name,DIRECTORY_SEPARATOR)){ |
|
| 51 | + static public function filePutContents(string $file_name, string $content, bool $file_append = false) { |
|
| 52 | + if (strrpos($file_name, DIRECTORY_SEPARATOR)) { |
|
| 53 | 53 | //获取文件夹路径 |
| 54 | - $dir_name = substr($file_name,0,strrpos($file_name,DIRECTORY_SEPARATOR)); |
|
| 54 | + $dir_name = substr($file_name, 0, strrpos($file_name, DIRECTORY_SEPARATOR)); |
|
| 55 | 55 | //创建文件夹 |
| 56 | 56 | self::mkdir($dir_name); |
| 57 | 57 | } |
@@ -60,10 +60,10 @@ discard block |
||
| 60 | 60 | self::chmod($file_name); |
| 61 | 61 | |
| 62 | 62 | //内容写入文件 |
| 63 | - if($file_append === false){ |
|
| 64 | - file_put_contents($file_name,$content); |
|
| 65 | - }else{ |
|
| 66 | - file_put_contents($file_name,$content,FILE_APPEND); |
|
| 63 | + if ($file_append === false) { |
|
| 64 | + file_put_contents($file_name, $content); |
|
| 65 | + } else { |
|
| 66 | + file_put_contents($file_name, $content, FILE_APPEND); |
|
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | 69 | |
@@ -76,10 +76,10 @@ discard block |
||
| 76 | 76 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
| 77 | 77 | * http://blog.majiameng.com/article/2724.html |
| 78 | 78 | */ |
| 79 | - static public function fWrite(string $file_name,string $content,bool $file_append = false){ |
|
| 80 | - if(strrpos($file_name,DIRECTORY_SEPARATOR)){ |
|
| 79 | + static public function fWrite(string $file_name, string $content, bool $file_append = false) { |
|
| 80 | + if (strrpos($file_name, DIRECTORY_SEPARATOR)) { |
|
| 81 | 81 | //获取文件夹路径 |
| 82 | - $dir_name = substr($file_name,0,strrpos($file_name,DIRECTORY_SEPARATOR)); |
|
| 82 | + $dir_name = substr($file_name, 0, strrpos($file_name, DIRECTORY_SEPARATOR)); |
|
| 83 | 83 | //创建文件夹 |
| 84 | 84 | self::mkdir($dir_name); |
| 85 | 85 | } |
@@ -88,11 +88,11 @@ discard block |
||
| 88 | 88 | self::chmod($file_name); |
| 89 | 89 | |
| 90 | 90 | //内容写入文件 |
| 91 | - if($file_append === false){ |
|
| 91 | + if ($file_append === false) { |
|
| 92 | 92 | $handle = fopen($file_name, 'w'); |
| 93 | 93 | fwrite($handle, $content); |
| 94 | 94 | fclose($handle); |
| 95 | - }else{ |
|
| 95 | + } else { |
|
| 96 | 96 | $handle = fopen($file_name, 'a'); |
| 97 | 97 | fwrite($handle, $content); |
| 98 | 98 | fclose($handle); |
@@ -105,9 +105,9 @@ discard block |
||
| 105 | 105 | * @param $dir_name |
| 106 | 106 | * @return bool |
| 107 | 107 | */ |
| 108 | - static public function mkdir(string $dir_name){ |
|
| 108 | + static public function mkdir(string $dir_name) { |
|
| 109 | 109 | if (!is_dir($dir_name)) { |
| 110 | - if(!mkdir($dir_name, 0755, true)){ |
|
| 110 | + if (!mkdir($dir_name, 0755, true)) { |
|
| 111 | 111 | die('创建缓存文件夹"'.$dir_name.'"失败!'); |
| 112 | 112 | } |
| 113 | 113 | //添加文件权限 |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | * @return bool |
| 124 | 124 | */ |
| 125 | 125 | static public function delDir(string $dir) { |
| 126 | - if(!file_exists($dir)){//文件不存在 |
|
| 126 | + if (!file_exists($dir)) {//文件不存在 |
|
| 127 | 127 | return true; |
| 128 | 128 | } |
| 129 | 129 | if (!is_dir($dir)) { |
@@ -134,9 +134,9 @@ discard block |
||
| 134 | 134 | //先删除目录下的文件 |
| 135 | 135 | $dh = opendir($dir); |
| 136 | 136 | while ($file = readdir($dh)) { |
| 137 | - if($file != "." && $file!="..") { |
|
| 137 | + if ($file != "." && $file != "..") { |
|
| 138 | 138 | $full_path = $dir."/".$file; |
| 139 | - if(!is_dir($full_path)) { |
|
| 139 | + if (!is_dir($full_path)) { |
|
| 140 | 140 | unlink($full_path); |
| 141 | 141 | } else { |
| 142 | 142 | self::delDir($full_path); |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | closedir($dh); |
| 147 | 147 | |
| 148 | 148 | //删除当前文件夹: |
| 149 | - if(rmdir($dir)) { |
|
| 149 | + if (rmdir($dir)) { |
|
| 150 | 150 | return true; |
| 151 | 151 | } else { |
| 152 | 152 | return false; |
@@ -159,9 +159,9 @@ discard block |
||
| 159 | 159 | * @param $file_name |
| 160 | 160 | * @param int $mode |
| 161 | 161 | */ |
| 162 | - static public function chmod($file_name,$mode = 0755){ |
|
| 163 | - if (file_exists($file_name)){ |
|
| 164 | - @chmod($file_name,$mode); |
|
| 162 | + static public function chmod($file_name, $mode = 0755) { |
|
| 163 | + if (file_exists($file_name)) { |
|
| 164 | + @chmod($file_name, $mode); |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
@@ -172,12 +172,12 @@ discard block |
||
| 172 | 172 | * dirname()可以的当前文件目录 |
| 173 | 173 | * @return array |
| 174 | 174 | */ |
| 175 | - static public function scanDir($path){ |
|
| 175 | + static public function scanDir($path) { |
|
| 176 | 176 | $filename = scandir($path); |
| 177 | 177 | $result = array(); |
| 178 | - foreach($filename as $k=>$v){ |
|
| 178 | + foreach ($filename as $k=>$v) { |
|
| 179 | 179 | // 跳过两个特殊目录 continue跳出循环 |
| 180 | - if($v=="." || $v==".."){continue;} |
|
| 180 | + if ($v == "." || $v == "..") {continue; } |
|
| 181 | 181 | $result[] = $v; |
| 182 | 182 | } |
| 183 | 183 | return $result; |
@@ -193,21 +193,21 @@ discard block |
||
| 193 | 193 | static public function move(string $file, string $new_file): bool |
| 194 | 194 | { |
| 195 | 195 | //文件是否存在 |
| 196 | - if(!file_exists($file)){ |
|
| 196 | + if (!file_exists($file)) { |
|
| 197 | 197 | return false; |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | //新文件目录 |
| 201 | - if(strrpos($new_file,DIRECTORY_SEPARATOR)){ |
|
| 201 | + if (strrpos($new_file, DIRECTORY_SEPARATOR)) { |
|
| 202 | 202 | //获取文件夹路径 |
| 203 | - $dir_name = substr($new_file,0,strrpos($new_file,DIRECTORY_SEPARATOR)); |
|
| 203 | + $dir_name = substr($new_file, 0, strrpos($new_file, DIRECTORY_SEPARATOR)); |
|
| 204 | 204 | //创建文件夹 |
| 205 | 205 | self::mkdir($dir_name); |
| 206 | 206 | //添加文件权限 |
| 207 | 207 | self::chmod($dir_name); |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - copy($file,$new_file); //拷贝到新目录 |
|
| 210 | + copy($file, $new_file); //拷贝到新目录 |
|
| 211 | 211 | unlink($file); //删除旧目录下的文件 |
| 212 | 212 | |
| 213 | 213 | return true; |
@@ -19,10 +19,10 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | public $client; |
| 21 | 21 | |
| 22 | - static public function init(){ |
|
| 22 | + static public function init() { |
|
| 23 | 23 | $gateway = new self(); |
| 24 | 24 | |
| 25 | - $gateway->client = new ZipArchive(); |
|
| 25 | + $gateway->client = new ZipArchive(); |
|
| 26 | 26 | return $gateway; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -33,29 +33,29 @@ discard block |
||
| 33 | 33 | * @author: Tinymeng <[email protected]> |
| 34 | 34 | * @time: 2022/4/27 9:26 |
| 35 | 35 | */ |
| 36 | - public function open($filename,$Fromfilename=null){ |
|
| 37 | - if (!empty($filename)){ |
|
| 36 | + public function open($filename, $Fromfilename = null) { |
|
| 37 | + if (!empty($filename)) { |
|
| 38 | 38 | $this->setFileName($filename); |
| 39 | - if(!$this->client->open($this->fileName, ZipArchive::CREATE)) |
|
| 39 | + if (!$this->client->open($this->fileName, ZipArchive::CREATE)) |
|
| 40 | 40 | return 'File open failed'; |
| 41 | 41 | } |
| 42 | - if(!empty($Fromfilename)){ |
|
| 43 | - if(is_dir($Fromfilename)){ |
|
| 42 | + if (!empty($Fromfilename)) { |
|
| 43 | + if (is_dir($Fromfilename)) { |
|
| 44 | 44 | $list_dir = scandir($Fromfilename); |
| 45 | - for($i=2;$i<count($list_dir);$i++){ |
|
| 46 | - if(is_dir($Fromfilename.'/'.$list_dir[$i])){ |
|
| 45 | + for ($i = 2; $i < count($list_dir); $i++) { |
|
| 46 | + if (is_dir($Fromfilename.'/'.$list_dir[$i])) { |
|
| 47 | 47 | $this->client->addGlob($Fromfilename.'/'.$list_dir[$i].'/*.*', 0, array('add_path' => $Fromfilename.'/'.$list_dir[$i].'/', 'remove_path' => $Fromfilename.'/'.$list_dir[$i])); |
| 48 | 48 | $list_chr = scandir($Fromfilename.'/'.$list_dir[$i]); |
| 49 | - for($j=2;$j<count($list_chr);$j++){ |
|
| 50 | - if(is_dir($Fromfilename.'/'.$list_dir[$i].'/'.$list_chr[$j])){ |
|
| 49 | + for ($j = 2; $j < count($list_chr); $j++) { |
|
| 50 | + if (is_dir($Fromfilename.'/'.$list_dir[$i].'/'.$list_chr[$j])) { |
|
| 51 | 51 | echo $list_chr[$j]; |
| 52 | - $this->open('',$Fromfilename.'/'.$list_dir[$i].'/'.$list_chr[$j]); |
|
| 52 | + $this->open('', $Fromfilename.'/'.$list_dir[$i].'/'.$list_chr[$j]); |
|
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | - }else{ |
|
| 58 | - $this->client->addFile ($Fromfilename); |
|
| 57 | + } else { |
|
| 58 | + $this->client->addFile($Fromfilename); |
|
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | 61 | return $this; |
@@ -68,9 +68,9 @@ discard block |
||
| 68 | 68 | * @author: Tinymeng <[email protected]> |
| 69 | 69 | * @time: 2022/4/27 9:26 |
| 70 | 70 | */ |
| 71 | - private function setFileName($filename){ |
|
| 72 | - if(!preg_match('/zip/m', $filename)){ |
|
| 73 | - $filename = $filename.'-'.date('Ymd').rand(111,999).'.zip'; |
|
| 71 | + private function setFileName($filename) { |
|
| 72 | + if (!preg_match('/zip/m', $filename)) { |
|
| 73 | + $filename = $filename.'-'.date('Ymd').rand(111, 999).'.zip'; |
|
| 74 | 74 | } |
| 75 | 75 | $this->fileName = $filename; |
| 76 | 76 | return $filename; |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * @author: Tinymeng <[email protected]> |
| 83 | 83 | * @time: 2022/4/27 9:50 |
| 84 | 84 | */ |
| 85 | - public function getFileName(){ |
|
| 85 | + public function getFileName() { |
|
| 86 | 86 | return $this->fileName; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -94,8 +94,8 @@ discard block |
||
| 94 | 94 | * @author: Tinymeng <[email protected]> |
| 95 | 95 | * @time: 2022/4/27 9:31 |
| 96 | 96 | */ |
| 97 | - public function addFileContent($file_name,$content){ |
|
| 98 | - $this->client->addFromString($file_name,$content); |
|
| 97 | + public function addFileContent($file_name, $content) { |
|
| 98 | + $this->client->addFromString($file_name, $content); |
|
| 99 | 99 | return $this; |
| 100 | 100 | } |
| 101 | 101 | |
@@ -107,8 +107,8 @@ discard block |
||
| 107 | 107 | * @author: Tinymeng <[email protected]> |
| 108 | 108 | * @time: 2022/4/27 9:31 |
| 109 | 109 | */ |
| 110 | - public function addFile($file_name,$content){ |
|
| 111 | - $this->client->addFromString($file_name,$content); |
|
| 110 | + public function addFile($file_name, $content) { |
|
| 111 | + $this->client->addFromString($file_name, $content); |
|
| 112 | 112 | return $this; |
| 113 | 113 | } |
| 114 | 114 | |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | * 文件下载 |
| 117 | 117 | * @return void |
| 118 | 118 | */ |
| 119 | - public function download(){ |
|
| 119 | + public function download() { |
|
| 120 | 120 | $this->client->finish(); |
| 121 | 121 | } |
| 122 | 122 | |
@@ -128,17 +128,17 @@ discard block |
||
| 128 | 128 | * @author: Tinymeng <[email protected]> |
| 129 | 129 | * @time: 2022/4/27 9:38 |
| 130 | 130 | */ |
| 131 | - public function unzip($filename,$dir){ |
|
| 132 | - if(!file_exists($filename)) |
|
| 131 | + public function unzip($filename, $dir) { |
|
| 132 | + if (!file_exists($filename)) |
|
| 133 | 133 | return 'File does not exist'; |
| 134 | - if(!$this->client->open($filename)) |
|
| 134 | + if (!$this->client->open($filename)) |
|
| 135 | 135 | return 'File open failed'; |
| 136 | - if(!is_dir($dir)){ |
|
| 137 | - mkdir($dir,775); |
|
| 138 | - }else{ |
|
| 136 | + if (!is_dir($dir)) { |
|
| 137 | + mkdir($dir, 775); |
|
| 138 | + } else { |
|
| 139 | 139 | return 'Dir mk failed'; |
| 140 | 140 | } |
| 141 | - if(!$this->client->extractTo($dir)) |
|
| 141 | + if (!$this->client->extractTo($dir)) |
|
| 142 | 142 | return 'File unzip failed'; |
| 143 | 143 | return $this; |
| 144 | 144 | } |
@@ -13,11 +13,11 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | private static $instance = null; |
| 15 | 15 | |
| 16 | - public function __construct($config=null){ |
|
| 16 | + public function __construct($config = null) { |
|
| 17 | 17 | $this->config = $config; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - private function __clone(){} |
|
| 20 | + private function __clone() {} |
|
| 21 | 21 | |
| 22 | 22 | public function __sleep(): array |
| 23 | 23 | { |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | return []; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - protected static function init($gateway, $config=null) |
|
| 28 | + protected static function init($gateway, $config = null) |
|
| 29 | 29 | { |
| 30 | 30 | if (!self::$instance instanceof static) { |
| 31 | 31 | self::$instance = new static($config); |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * @param $config |
| 42 | 42 | * @return mixed |
| 43 | 43 | */ |
| 44 | - public static function __callStatic($gateway, $config=null) |
|
| 44 | + public static function __callStatic($gateway, $config = null) |
|
| 45 | 45 | { |
| 46 | 46 | return self::init($gateway, ...$config); |
| 47 | 47 | } |