deepeloper / lib-fs
文件系统库
3.0.0
2023-06-22 21:51 UTC
Requires
- php: >=8.1
Requires (Dev)
README
安装
运行 composer require deepeloper/lib-fs
.
用法
工具:递归遍历目录
\deepeloper\Lib\FileSystem\Tools::walkDir( "/path/to/dir", function (\SplFileInfo $file, $key, array $args) { // $args["path"] contains passed "/path/to/dir" ($path) echo sprintf( "[%s] %s%s", $file->isDir() ? "DIR " : "file", $file->getRealPath(), PHP_EOL ); } );
工具:递归删除目录
\deepeloper\Lib\FileSystem\Tools::removeDir("/path/to/dir"); // clearstatcache(...);
工具:递归搜索与替换
\deepeloper\Lib\FileSystem\Tools::search( "/path/to/dir", 0, // Flags (php://glob()) ["*", ".*"], // File name patterns (php://glob()) ["*", ".*"], // Subdir name patterns (php://glob()) "needle", // String to search in files, if starts with "/" processes like regular expression function ($path, array $args) { // $args['path'] contains passed "/path/to/dir" ($dir) // $args['needle'] contains passed "needle" ($needle) $contents = file_get_contents($path); $contents = preg_replace("/needle/", "replacement", $contents); file_put_contents($path, $contents); } );
支持文件轮转的日志功能
$logger = new \deepeloper\Lib\FileSystem\Logger([ 'path' => "/path/to/log", // 'maxSize' => int maxSize, // Logger::DEFAULT_MAX_SIZE by default. // 'rotation' => int rotation, // Rotating files number, 0 means no rotation. // 'rights' => int rights, // If set after writing to log file chmod() will be called. ]); $logger->log("Foo");