当前位置:网站首页>[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:
- file:// — access the local file system
- http:// — Access HTTP(s) URL
- ftp:// — Access FTP(s) URLs
- php:// — access each input/I/O streams
- zlib:// — Compressed Streamli>
- data:// — Data (RFC 2397)
- glob:// — find matching filesPath Pattern
- phar:// — PHP Archiveli>
- ssh2:// — Secure Shell 2
- rar:// — RAR
- ogg:// — Audio Streamingli>
- expect:// — handles interactiveStream
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');
边栏推荐
猜你喜欢
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
TypeScript 错误 error TS2469、error TS2731 解决办法
v-bind用法:类动态绑定对象 数组 style样式 及函数方法
稳定好用的短连接生成平台,支持API批量生成
正则笔记(2)- 正则表达式位置匹配攻略
PHP入门(自学笔记)
MySql高级 -- 约束
[league/climate] A robust command-line function manipulation library
npm --package.json---require
[league/climate]一个功能健全的命令行功能操作库
随机推荐
About the apache .htaccess file of tp
4.PHP数组与数组排序
13. JS output content and syntax
js 中this指向
js basics
(3)Thinkphp6数据库
PHP有哪些框架?
AES加密的各种蛋疼方式方式
When PHP initiates Alipay payment, the order information is garbled and solved
meime module
解决 Zlibrary 卡死/找不到域名/达到限额问题,Zlibrary最新地址
查询数据库中所有表的索引,并且解析成sql
1.10今日学习
每日五道面试题总结 22/7/21
Thread Pool (Introduction and Use of Thread Pool)
SQL分类、DQL(数据查询语言)、以及相应SQL查询语句演示
3.PHP数据类型、常量、字符串和运算符
阿里云设置域名解析重定向后,无法使用Chrome访问
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
逍遥多开模拟器ADB驱动连接