Run 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");