当前位置:网站首页>[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');边栏推荐
猜你喜欢

(3) Thinkphp6 database

SQL classification, DQL (Data Query Language), and corresponding SQL query statement demonstration

点名系统和数组元素为对象的排序求最大值和最小值

js basics

hackmyvm-hopper walkthrough
![[league/climate] A robust command-line function manipulation library](/img/ce/39114b1c74af649223db97e5b0e29c.png)
[league/climate] A robust command-line function manipulation library

解决5+APP真机测试无法访问后台(同局域网)

(3)Thinkphp6数据库

New usage of string variable parsing in PHP8.2

What are the PHP framework?
随机推荐
js 之 Object.defineProperty()
12.什么是JS
5.20今日学习
4.表单与输入
6.27面试集
1.初识PHP
4.PHP数组与数组排序
--fs module--
PHP 发起支付宝支付时 订单信息乱码解决
[mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
面试总结 22/7/22 面试中的重点
IO流、 编码表、 字符流、 字符缓冲流
[symfony/finder]最好用的文件操作库
1.uview form校验位置可以改变 2.时间区间
js basics
Add a full image watermark to an image in PHP
js的“类数组”及“类数组转数组”
[campo/random-user-agent]随机伪造你的User-Agent
使用PHPMailer发送邮件
MySql Advanced -- Constraints