当前位置:网站首页>[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');边栏推荐
猜你喜欢
随机推荐
uniapp | 开发中遇到的兼容性问题(待续)
Phpstudy installs Thinkphp6 (problem + solution)
TCP communications program
如何计算地球上两点的距离(附公式推导)
PHP的几个有趣的打开方式:从基本到变态
点名系统和数组元素为对象的排序求最大值和最小值
面试总结 22/7/22 面试中的重点
PHP 发起支付宝支付时 订单信息乱码解决
PHP8.2的版本发布管理员和发布计划
正则笔记(1)- 正则表达式字符匹配攻略
微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
正则笔记(2)- 正则表达式位置匹配攻略
[phpunit/php-timer]一个用于代码执行时间的计时器
6.24今日学习
Various ways of AES encryption
After the mailbox of the Pagoda Post Office is successfully set up, it can be sent but not received.
SQL分类、DQL(数据查询语言)、以及相应SQL查询语句演示
js的“类数组”及“类数组转数组”
微信小程序自定义swiper轮播图面板指示点|小圆点|进度条
easyswoole uses redis to perform geoRadiusByMember Count invalid fix









