@@ -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,27 +16,27 @@ 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 | list($msec, $sec) = explode(' ', microtime()); |
| 24 | - $message = date('Y-m-d H:i:s',$sec).'.'.str_replace('0.','',$msec).' : '.$message.PHP_EOL; |
|
| 25 | - if($exception && $exception instanceof \Exception){ |
|
| 24 | + $message = date('Y-m-d H:i:s', $sec).'.'.str_replace('0.', '', $msec).' : '.$message.PHP_EOL; |
|
| 25 | + if ($exception && $exception instanceof \Exception) { |
|
| 26 | 26 | $message .= ' File: '.$exception->getFile().' ,Line: '.$exception->getLine().' ,Message: '.$exception->getMessage(); |
| 27 | 27 | } |
| 28 | - if($echo){ |
|
| 28 | + if ($echo) { |
|
| 29 | 29 | echo $message; |
| 30 | 30 | } |
| 31 | 31 | $path = logFilePath; |
| 32 | 32 | if (!is_dir($path)) { |
| 33 | - if(!mkdir($path, 0755, true)){ |
|
| 33 | + if (!mkdir($path, 0755, true)) { |
|
| 34 | 34 | die('创建缓存文件夹"'.$path.'"失败!'); |
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | $file_name = $path.$file_name; |
| 39 | - self::filePutContents($file_name."-".date('Ymd',time()).".log",$message,true); |
|
| 39 | + self::filePutContents($file_name."-".date('Ymd', time()).".log", $message, true); |
|
| 40 | 40 | return true; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -49,10 +49,10 @@ discard block |
||
| 49 | 49 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
| 50 | 50 | * http://blog.majiameng.com/article/2724.html |
| 51 | 51 | */ |
| 52 | - static public function filePutContents(string $file_name,string $content,bool $file_append = false){ |
|
| 53 | - if(strrpos($file_name,'/')){ |
|
| 52 | + static public function filePutContents(string $file_name, string $content, bool $file_append = false) { |
|
| 53 | + if (strrpos($file_name, '/')) { |
|
| 54 | 54 | //获取文件夹路径 |
| 55 | - $dir_name = substr($file_name,0,strrpos($file_name,'/')); |
|
| 55 | + $dir_name = substr($file_name, 0, strrpos($file_name, '/')); |
|
| 56 | 56 | //创建文件夹 |
| 57 | 57 | self::mkdir($dir_name); |
| 58 | 58 | } |
@@ -61,10 +61,10 @@ discard block |
||
| 61 | 61 | self::chmod($file_name); |
| 62 | 62 | |
| 63 | 63 | //内容写入文件 |
| 64 | - if($file_append === false){ |
|
| 65 | - file_put_contents($file_name,$content); |
|
| 66 | - }else{ |
|
| 67 | - file_put_contents($file_name,$content,FILE_APPEND); |
|
| 64 | + if ($file_append === false) { |
|
| 65 | + file_put_contents($file_name, $content); |
|
| 66 | + } else { |
|
| 67 | + file_put_contents($file_name, $content, FILE_APPEND); |
|
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | |
@@ -77,10 +77,10 @@ discard block |
||
| 77 | 77 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
| 78 | 78 | * http://blog.majiameng.com/article/2724.html |
| 79 | 79 | */ |
| 80 | - static public function fWrite(string $file_name,string $content,bool $file_append = false){ |
|
| 81 | - if(strrpos($file_name,'/')){ |
|
| 80 | + static public function fWrite(string $file_name, string $content, bool $file_append = false) { |
|
| 81 | + if (strrpos($file_name, '/')) { |
|
| 82 | 82 | //获取文件夹路径 |
| 83 | - $dir_name = substr($file_name,0,strrpos($file_name,'/')); |
|
| 83 | + $dir_name = substr($file_name, 0, strrpos($file_name, '/')); |
|
| 84 | 84 | //创建文件夹 |
| 85 | 85 | self::mkdir($dir_name); |
| 86 | 86 | } |
@@ -89,11 +89,11 @@ discard block |
||
| 89 | 89 | self::chmod($file_name); |
| 90 | 90 | |
| 91 | 91 | //内容写入文件 |
| 92 | - if($file_append === false){ |
|
| 92 | + if ($file_append === false) { |
|
| 93 | 93 | $handle = fopen($file_name, 'w'); |
| 94 | 94 | fwrite($handle, $content); |
| 95 | 95 | fclose($handle); |
| 96 | - }else{ |
|
| 96 | + } else { |
|
| 97 | 97 | $handle = fopen($file_name, 'a'); |
| 98 | 98 | fwrite($handle, $content); |
| 99 | 99 | fclose($handle); |
@@ -107,9 +107,9 @@ discard block |
||
| 107 | 107 | * @param $dir_name |
| 108 | 108 | * @return bool |
| 109 | 109 | */ |
| 110 | - static public function mkdir(string $dir_name){ |
|
| 110 | + static public function mkdir(string $dir_name) { |
|
| 111 | 111 | if (!is_dir($dir_name)) { |
| 112 | - if(!mkdir($dir_name, 0755, true)){ |
|
| 112 | + if (!mkdir($dir_name, 0755, true)) { |
|
| 113 | 113 | die('创建缓存文件夹"'.$dir_name.'"失败!'); |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -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 | |
@@ -175,14 +175,14 @@ discard block |
||
| 175 | 175 | static public function move(string $file, string $new_file): bool |
| 176 | 176 | { |
| 177 | 177 | //文件是否存在 |
| 178 | - if(!file_exists($file)){ |
|
| 178 | + if (!file_exists($file)) { |
|
| 179 | 179 | return false; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | //新文件目录 |
| 183 | - if(strrpos($new_file,'/')){ |
|
| 183 | + if (strrpos($new_file, '/')) { |
|
| 184 | 184 | //获取文件夹路径 |
| 185 | - $dir_name = substr($new_file,0,strrpos($new_file,'/')); |
|
| 185 | + $dir_name = substr($new_file, 0, strrpos($new_file, '/')); |
|
| 186 | 186 | //创建文件夹 |
| 187 | 187 | self::mkdir($dir_name); |
| 188 | 188 | } |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | //添加文件权限 |
| 191 | 191 | self::chmod($dir_name); |
| 192 | 192 | |
| 193 | - copy($file,$new_file); //拷贝到新目录 |
|
| 193 | + copy($file, $new_file); //拷贝到新目录 |
|
| 194 | 194 | unlink($file); //删除旧目录下的文件 |
| 195 | 195 | |
| 196 | 196 | return true; |