majiameng /
tools
| 1 | <?php |
||||
| 2 | namespace tinymeng\tools; |
||||
| 3 | /** |
||||
| 4 | * 文件操作类 |
||||
| 5 | * logFilePath: /storage/tinymeng/log/ |
||||
| 6 | */ |
||||
| 7 | define('logFilePath',dirname(dirname(dirname(dirname(__DIR__)))).DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'tinymeng'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR); |
||||
| 8 | class FileTool{ |
||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * Name: writeLog |
||||
| 12 | * Author: Tinymeng <[email protected]> |
||||
| 13 | * @param string $message 日志信息 |
||||
| 14 | * @param string $file_name 文件名称 |
||||
| 15 | * @param bool $echo 是否输出 |
||||
| 16 | * @param \Exception|null $exception |
||||
| 17 | * @return bool |
||||
| 18 | */ |
||||
| 19 | static public function writeLog($message, $file_name='error',bool $echo = false,\Exception $exception = null){ |
||||
| 20 | if(!is_string($message)){ |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 21 | $message = json_encode($message); |
||||
| 22 | } |
||||
| 23 | $message = date('Y-m-d H:i:s').' : '.$message.PHP_EOL; |
||||
| 24 | if($exception && $exception instanceof \Exception){ |
||||
| 25 | $message .= ' File: '.$exception->getFile().' ,Line: '.$exception->getLine().' ,Message: '.$exception->getMessage(); |
||||
| 26 | } |
||||
| 27 | if($echo){ |
||||
| 28 | echo $message; |
||||
| 29 | } |
||||
| 30 | $path = logFilePath; |
||||
| 31 | if (!is_dir($path)) { |
||||
| 32 | if(!mkdir($path, 0755, true)){ |
||||
| 33 | die('创建缓存文件夹"'.$path.'"失败!'); |
||||
|
0 ignored issues
–
show
|
|||||
| 34 | } |
||||
| 35 | } |
||||
| 36 | |||||
| 37 | $file_name = $path.$file_name; |
||||
| 38 | self::filePutContents($file_name."-".date('Ymd',time()).".log",$message,true); |
||||
| 39 | return true; |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Name: 写入文件(数据小使用file_put_contents) |
||||
| 44 | * Author: Tinymeng <[email protected]> |
||||
| 45 | * @param string $file_name 文件名称 |
||||
| 46 | * @param string $content 文件内容 |
||||
| 47 | * @param bool $file_append 内容是否追加 |
||||
| 48 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
||||
| 49 | * http://blog.majiameng.com/article/2724.html |
||||
| 50 | */ |
||||
| 51 | static public function filePutContents(string $file_name,string $content,bool $file_append = false){ |
||||
| 52 | if(strrpos($file_name,DIRECTORY_SEPARATOR)){ |
||||
| 53 | //获取文件夹路径 |
||||
| 54 | $dir_name = substr($file_name,0,strrpos($file_name,DIRECTORY_SEPARATOR)); |
||||
| 55 | //创建文件夹 |
||||
| 56 | self::mkdir($dir_name); |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | //添加文件权限 |
||||
| 60 | self::chmod($file_name); |
||||
| 61 | |||||
| 62 | //内容写入文件 |
||||
| 63 | if($file_append === false){ |
||||
| 64 | file_put_contents($file_name,$content); |
||||
| 65 | }else{ |
||||
| 66 | file_put_contents($file_name,$content,FILE_APPEND); |
||||
| 67 | } |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | /** |
||||
| 71 | * Name: 写入文件(文件大,高并发使用fwrite写入) |
||||
| 72 | * Author: Tinymeng <[email protected]> |
||||
| 73 | * @param string $file_name 文件名称 |
||||
| 74 | * @param string $content 文件内容 |
||||
| 75 | * @param bool $file_append 内容是否追加 |
||||
| 76 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
||||
| 77 | * http://blog.majiameng.com/article/2724.html |
||||
| 78 | */ |
||||
| 79 | static public function fWrite(string $file_name,string $content,bool $file_append = false){ |
||||
| 80 | if(strrpos($file_name,DIRECTORY_SEPARATOR)){ |
||||
| 81 | //获取文件夹路径 |
||||
| 82 | $dir_name = substr($file_name,0,strrpos($file_name,DIRECTORY_SEPARATOR)); |
||||
| 83 | //创建文件夹 |
||||
| 84 | self::mkdir($dir_name); |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | //添加文件权限 |
||||
| 88 | self::chmod($file_name); |
||||
| 89 | |||||
| 90 | //内容写入文件 |
||||
| 91 | if($file_append === false){ |
||||
| 92 | $handle = fopen($file_name, 'w'); |
||||
| 93 | fwrite($handle, $content); |
||||
| 94 | fclose($handle); |
||||
| 95 | }else{ |
||||
| 96 | $handle = fopen($file_name, 'a'); |
||||
| 97 | fwrite($handle, $content); |
||||
| 98 | fclose($handle); |
||||
| 99 | } |
||||
| 100 | } |
||||
| 101 | |||||
| 102 | /** |
||||
| 103 | * Name: 创建文件夹 |
||||
| 104 | * Author: Tinymeng <[email protected]> |
||||
| 105 | * @param $dir_name |
||||
| 106 | * @return bool |
||||
| 107 | */ |
||||
| 108 | static public function mkdir(string $dir_name){ |
||||
| 109 | if (!is_dir($dir_name)) { |
||||
| 110 | if(!mkdir($dir_name, 0755, true)){ |
||||
| 111 | die('创建缓存文件夹"'.$dir_name.'"失败!'); |
||||
|
0 ignored issues
–
show
|
|||||
| 112 | } |
||||
| 113 | //添加文件权限 |
||||
| 114 | self::chmod($dir_name); |
||||
| 115 | } |
||||
| 116 | return true; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * Name: 删除文件夹或文件 |
||||
| 121 | * Author: Tinymeng <[email protected]> |
||||
| 122 | * @param $dir |
||||
| 123 | * @return bool |
||||
| 124 | */ |
||||
| 125 | static public function delDir(string $dir) { |
||||
| 126 | if(!file_exists($dir)){//文件不存在 |
||||
| 127 | return true; |
||||
| 128 | } |
||||
| 129 | if (!is_dir($dir)) { |
||||
| 130 | unlink($dir); |
||||
| 131 | return true; |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | //先删除目录下的文件 |
||||
| 135 | $dh = opendir($dir); |
||||
| 136 | while ($file = readdir($dh)) { |
||||
| 137 | if($file != "." && $file!="..") { |
||||
| 138 | $full_path = $dir."/".$file; |
||||
| 139 | if(!is_dir($full_path)) { |
||||
| 140 | unlink($full_path); |
||||
| 141 | } else { |
||||
| 142 | self::delDir($full_path); |
||||
| 143 | } |
||||
| 144 | } |
||||
| 145 | } |
||||
| 146 | closedir($dh); |
||||
| 147 | |||||
| 148 | //删除当前文件夹: |
||||
| 149 | if(rmdir($dir)) { |
||||
| 150 | return true; |
||||
| 151 | } else { |
||||
| 152 | return false; |
||||
| 153 | } |
||||
| 154 | } |
||||
| 155 | |||||
| 156 | /** |
||||
| 157 | * 给目录赋值权限 |
||||
| 158 | * @Author: TinyMeng <[email protected]> |
||||
| 159 | * @param $file_name |
||||
| 160 | * @param int $mode |
||||
| 161 | */ |
||||
| 162 | static public function chmod($file_name,$mode = 0755){ |
||||
| 163 | if (file_exists($file_name)){ |
||||
| 164 | @chmod($file_name,$mode); |
||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
chmod(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||
| 165 | } |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * 获取目录文件 |
||||
| 170 | * @Author: TinyMeng <[email protected]> |
||||
| 171 | * @param $path |
||||
| 172 | * dirname()可以的当前文件目录 |
||||
| 173 | * @return array |
||||
| 174 | */ |
||||
| 175 | static public function scanDir($path){ |
||||
| 176 | $filename = scandir($path); |
||||
| 177 | $result = array(); |
||||
| 178 | foreach($filename as $k=>$v){ |
||||
| 179 | // 跳过两个特殊目录 continue跳出循环 |
||||
| 180 | if($v=="." || $v==".."){continue;} |
||||
| 181 | $result[] = $v; |
||||
| 182 | } |
||||
| 183 | return $result; |
||||
| 184 | } |
||||
| 185 | |||||
| 186 | /** |
||||
| 187 | * 文件移动 |
||||
| 188 | * @Author: TinyMeng <[email protected]> |
||||
| 189 | * @param string $file old文件 |
||||
| 190 | * @param string $new_file 新文件 |
||||
| 191 | * @return bool |
||||
| 192 | */ |
||||
| 193 | static public function move(string $file, string $new_file): bool |
||||
| 194 | { |
||||
| 195 | //文件是否存在 |
||||
| 196 | if(!file_exists($file)){ |
||||
| 197 | return false; |
||||
| 198 | } |
||||
| 199 | |||||
| 200 | //新文件目录 |
||||
| 201 | if(strrpos($new_file,DIRECTORY_SEPARATOR)){ |
||||
| 202 | //获取文件夹路径 |
||||
| 203 | $dir_name = substr($new_file,0,strrpos($new_file,DIRECTORY_SEPARATOR)); |
||||
| 204 | //创建文件夹 |
||||
| 205 | self::mkdir($dir_name); |
||||
| 206 | //添加文件权限 |
||||
| 207 | self::chmod($dir_name); |
||||
| 208 | } |
||||
| 209 | |||||
| 210 | copy($file,$new_file); //拷贝到新目录 |
||||
| 211 | unlink($file); //删除旧目录下的文件 |
||||
| 212 | |||||
| 213 | return true; |
||||
| 214 | } |
||||
| 215 | } |
||||
| 216 |