当前位置:网站首页>[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');
边栏推荐
- easyswoole 使用redis执行geoRadiusByMember Count无效修复
- 微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
- 每日五道面试题总结 22/7/26
- 14. JS Statements and Comments, Variables and Data Types
- QR code generation API interface, which can be directly connected as an A tag
- 你的本地创建的项目库还在手动创建远端代码仓库再推送吗,该用它了
- js 正则中 replace() 使用
- 1.10今日学习
- PHP 发起支付宝支付时 订单信息乱码解决
- ES6数组的扩展方法map、filter、reduce、fill和数组遍历for…in for…of arr.forEach
猜你喜欢
js basics
简单黑马头条项目
MySql Advanced -- Constraints
js 原型和原型链
What are the PHP framework?
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
Stable and easy-to-use short connection generation platform, supporting API batch generation
PHP8.2中字符串变量解析的新用法
ES6三点运算符、数组方法、字符串扩展方法
面试总结 22/7/22 面试中的重点
随机推荐
Phpstudy安装Thinkphp6(问题+解决)
Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
项目中遇到的问题
About the apache .htaccess file of tp
PHP8.2中字符串变量解析的新用法
Baidu positioning js API
(5) 模块与包、编码格式、文件操作、目录操作
1.6一些今日学习
PHP image compression to specified size
uniapp | 开发中遇到的兼容性问题(待续)
[phpunit/php-timer]一个用于代码执行时间的计时器
Xiaoyao multi-open emulator ADB driver connection
微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
4.表单与输入
4.PHP数组与数组排序
IO stream, encoding table, character stream, character buffer stream
三月底啦啦
1.uview form校验位置可以改变 2.时间区间
vim编辑模式
轮播图详解(完整代码在最后)