PHPackages                             affinity4/file - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. affinity4/file

ActiveLibrary[File &amp; Storage](/categories/file-storage)

affinity4/file
==============

Recursively search child (or parent) directories of a given directory for files with regex pattern or filename

2.1.7(3y ago)232MITPHPPHP &gt;=7.3

Since Jan 8Pushed 3y ago2 watchersCompare

[ Source](https://github.com/affinity4/File)[ Packagist](https://packagist.org/packages/affinity4/file)[ RSS](/packages/affinity4-file/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (2)Versions (12)Used By (0)

File
====

[](#file)

[![Build Status](https://camo.githubusercontent.com/98017fdcc9777275b3e0f3bfb2d98142199dad8eab807cb82e618b280cef4e1a/68747470733a2f2f7472617669732d63692e6f72672f616666696e697479342f66696c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/affinity4/file)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/cd14d67da90b1f6d106897341f94990b3c65912e08e9babc73c949ec4c714465/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616666696e697479342f66696c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/affinity4/file/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/340de415ea54d9ae19f2ea0ba513b73018d742c9b953e35486da7d1fa4d0df82/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616666696e697479342f66696c652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/affinity4/file/?branch=master)

[![SensioLabsInsight](https://camo.githubusercontent.com/a5662b73b2e042e6923d6382f56664419ffef0e00ecea13b4fc13c8ef8d679be/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f32353831396139632d383765342d343839642d393033342d3738623432653664306538362f6269672e706e67)](https://insight.sensiolabs.com/projects/25819a9c-87e4-489d-9034-78b42e6d0e86)

Find files using a regex pattern or exact filename.

Features
--------

[](#features)

- Search the current directory for a file or files by exact filename or regex pattern
- Search the current directory and then the parent directory for a file or files using a filename or regex pattern
- Search the current directory and then recursively up through parent directories for a file or files using a filename or regex pattern.

Installation
------------

[](#installation)

Affinity4/File is available via composer:

`composer require affinity4/file`

or

```
{
    "require": {
        "affinity4/file": "^2.1"
    }
}

```

Usage
-----

[](#usage)

Assuming folder structure is:

```
root
  |-- files
  |    |-- config.yml
  |    |-- config.php
  |    |-- 00
  |    |    |-- test00-01.php
  |    |    |-- test00-01.yml
  |    |    |-- test00-02.php
  |    |    |-- test00-02.yml
  |    |    |-- 01
  |    |    |    |    |-- test01-01.php
  |    |    |    |    |-- test01-01.yml
  |    |    |    |    |-- test01-02.php
  |    |    |    |    |-- test01-02.yml
  |    |    |    |    |-- test01-03.php
  |    |    |    |    |-- test01-03.yml
  |    |    |    |    |-- 02
  |    |    |    |    |    |-- YOU-ARE-HERE
  |    |    |    |    |    |-- test02-01.html
  |    |    |    |    |    |-- test02-01.css
  |    |    |    |    |    |-- test02-01.json
  |    |    |    |    |    |-- test02-02.json

```

To find numerous files you can use a regex pattern with the following delimiters /, @, #, ~ in the `find()` method. Then chain the `in()` method to start the search from that directory and use `get()` to return the results:

```
$file = new Affinity4\File\File;
$results = $file->find('/^test[\d]{2}-[\d]{2}.json$/')->in(__DIR__)->get();

$results[0]->getPathname(); // root/files/00/01/02/test02-01.json
$results[1]->getPathname(); // root/files/00/01/02/test02-02.json

```

You can also search the current directory and if no files are found matching the pattern search one level up by chaining the `parent()` method after `in()`:

```
$file = new Affinity4\File\File;
$results = $file->find('/^test00-[\d]{2}.php$/')->in(__DIR__)->parent()->get();

$results[0]->getPathname(); // root/files/00/test00-01.php
$results[1]->getPathname(); // root/files/00/test00-02.php

```

You can also search the current directory and if no files are found matching the pattern search all parent directories by chaining the `parents()` method after `in()`:

```
$file = new Affinity4\File\File;
$result = $file->find('config.yml')->in(__DIR__)->parents()->get();

$result->getPathname(); // root/files/config.yml

```

You can also specify if you only want the one item returned using the `get()` method. This will return the first SplFileInfo object in the array, and not an array with one SplFileInfo object.

```
$file = new Affinity4\File\File;

$result = $file->find('/^test[\d]{2}-[\d]{2}.php$/')->in(__DIR__)->parents()->get(1);

return $result->getPathname() // root/files/00/01/test01-01.php;

```

You can also specify exactly how many items you want returned in an array using the `get()` method. As long as the number is greater than one the result will be an array of `SPLFileInfo` objects:

```
$file = new Affinity4\File\File;
$results = $file->find('/^test[\d]{2}-[\d]{2}.php$/')->in(__DIR__)->parents()->get(2);

$results[0]->getPathname(); // root/files/00/01/test01-01.php;
$results[1]->getPathname(); // root/files/00/01/test01-02.php;

```

Tests
-----

[](#tests)

Run tests:

```
vendor/bin/phpunit

```

Licence
-------

[](#licence)

(c) 2017 Luke Watts (Affinity4.ie)

This software is licensed under the MIT license. For the full copyright and license information, please view the LICENSE file that was distributed with this source code.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~219 days

Recently: every ~461 days

Total

10

Last Release

1442d ago

Major Versions

1.0.0 → 2.0.02017-01-10

PHP version history (4 changes)1.0.0PHP &gt;=5.4.0

2.0.0PHP &gt;=5.6.0

2.1.2PHP &gt;=7.0

2.1.7PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/6eda764179ed00e7d50099142dc56015a0a5a44542d6df9bb80fe087418efceb?d=identicon)[Affinity4](/maintainers/Affinity4)

---

Top Contributors

[![lukewatts](https://avatars.githubusercontent.com/u/4622166?v=4)](https://github.com/lukewatts "lukewatts (5 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

fileaffinity4

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/affinity4-file/health.svg)

```
[![Health](https://phpackages.com/badges/affinity4-file/health.svg)](https://phpackages.com/packages/affinity4-file)
```

###  Alternatives

[league/flysystem

File storage abstraction for PHP

13.6k639.1M2.2k](/packages/league-flysystem)[league/flysystem-aws-s3-v3

AWS S3 filesystem adapter for Flysystem.

1.6k263.6M790](/packages/league-flysystem-aws-s3-v3)[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[knplabs/knp-gaufrette-bundle

Allows to easily use the Gaufrette library in a Symfony project

72528.6M91](/packages/knplabs-knp-gaufrette-bundle)[league/flysystem-local

Local filesystem adapter for Flysystem.

226231.8M39](/packages/league-flysystem-local)[league/flysystem-memory

In-memory filesystem adapter for Flysystem.

8533.6M194](/packages/league-flysystem-memory)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
