当前位置:网站首页>[symfony/finder] The best file manipulation library

[symfony/finder] The best file manipulation library

2022-08-02 03:55:00 phpreturn

Sometimes, we need to operate the files on the server, either traverse the directory or find the corresponding extension files. At this time, the code we found on the Internet is very bad. Is there an elegant high-performance one?How?

finder is one such library.

Basic usage

use Symfony\Component\Finder\Finder;$finder = new Finder();// go to a folder$finder->files()->in(__DIR__);// check if it is emptyif ($finder->hasResults()) {// ...}foreach ($finder as $file) {// start traversing the file$absoluteFilePath = $file->getRealPath();$fileNameWithExtension = $file->getRelativePathname();// ...}

Elegant Filtered Search

Calling the in method in a chain means looking for another directory under a certain directory.

$finder->in(__DIR__)->in('/elsewhere');

Match the corresponding file by *

$finder->in('src/Symfony/*/*/Resources');

Exclude some files

$finder->in(__DIR__)->exclude('ruby');

Directly filter out files that do not have permission to read

$finder->ignoreUnreadableDirs()->in(__DIR__);

Support various underlying systems

Support access to standard file protocols

// Open an FTP address$finder->in('ftp://example.com/');$finder->in('ftp://example.com/pub/');

The following access methods are supported:

Supports custom underlying system protocols, the following example shows access to Amazon's storage.

use Symfony\Component\Finder\Finder;// Register access protocol through AWS official package 's3://'$s3Client = new Aws\S3\S3Client([/* config options */]);$s3Client->registerStreamWrapper();$finder = new Finder();$finder->name('photos*')->size('< 100K')->date('since 1 hour ago');foreach ($finder->in('s3://bucket-name') as $file) {// ... do something with the file}

Easy to manipulate files or directories

// only query files$finder->files();// only query the directory$finder->directories();

Filter files

Supports a very elegant way of manipulating files.

$finder->files()->name('*.php');$finder->files()->name('/\.php$/');$finder->files()->notName('*.rb');$finder->files()->size('>= 1K')->size('<= 2K');$finder->date('>= 2018-01-01')->date('<= 2018-12-31');

原网站

版权声明
本文为[phpreturn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020322218409.html