PHPackages                             lukaswhite/directory - 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. lukaswhite/directory

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

lukaswhite/directory
====================

A PHP class for manipulating a directory on a filesystem

1.0(5y ago)239MITPHP

Since Sep 27Pushed 5y ago2 watchersCompare

[ Source](https://github.com/lukaswhite/directory)[ Packagist](https://packagist.org/packages/lukaswhite/directory)[ RSS](/packages/lukaswhite-directory/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Directory
=========

[](#directory)

A PHP class that represents a directory on a filesystem.

With it, you can:

- Get the files in it, including `glob`-like patterns
- Find the most recent file
- Derive a unique filename based on a pattern
- Create or copy files in it
- Obtain the total size
- Delete it recursively

Creating a Instance
-------------------

[](#creating-a-instance)

Simply path the full path to the directory to the constructor:

```
$directory = new Directory( '/path/to/dir' );
```

Checking it Exists
------------------

[](#checking-it-exists)

The directory needn't exist when you create an instance; use the `exists()` method to check that it does.

```
if ( $directory->exists ) {
	// do something
}
```

Checking it's a Directory
-------------------------

[](#checking-its-a-directory)

It might also a good idea to check that the path you provided is actually a directory, not a file.

```
if ( ! $directory->isDirectory( ) ) {
	// looks like it's a file!
}
```

Creating it
-----------

[](#creating-it)

To create a directory, pass the path to the constructor and then call `create()`.

```
$directory = new Directory( '/path/to/new/directory' );
$directory->create( );
```

By default the mode is set to `0777`, but you can override this by passing it as an argument:

```
$directory->create( 0755 );
```

Alternatively, you can use `createIfDoesNotExist()` which, as the name suggests, will create it if it does not already exist.

> This action is recusrive; i.e. it will create any necessary parent directories.

Listing Files
-------------

[](#listing-files)

Call `getFiles()` to get a list of the files in the directory.

```
$files = $directory->getFiles( );
```

This will return an array with the full path to the files in the directory, excluding directories.

To include the directories:

```
$files = $directory->getFiles( true );
```

To get all of the files in a directory including any subdirectories, pass `true` as the second argument:

```
$files = $directory->getFiles( false, true );
```

Glob
----

[](#glob)

To glob the directory:

```
$textFiles = $directory->glob( '*.txt' );
```

Checking if a File Exists
-------------------------

[](#checking-if-a-file-exists)

To check whether a directory contains a file with a particular name:

```
if ( $directory->fileExists( 'logo.png' ) ) {
	// do something
}
```

Getting the Most Recent file
----------------------------

[](#getting-the-most-recent-file)

Use `mostRecentFile()` to get the most recently modified file.

```
$recent = $directory->mostRecentFile( );
```

You can use a pattern; for example to get the most recently modified text file:

```
$recent = $directory->mostRecentFile( '*.txt' );
```

To include directories, pass `true` as the second argument.

Getting the Total Size
----------------------

[](#getting-the-total-size)

To get the total size, in bytes, of a directory:

```
$size = $directory->totalSize( );
```

Unique Filenames
----------------

[](#unique-filenames)

Suppose you allow users to upload an avatar, which you store in a directory named `avatars` with the filename in the form `username.png`.

That works fine initially, but causes problems if a user uploads a replacement.

To get around that, `ensureUniqueFilename()` will return a similar filename that doesn't exist.

For example, if `joebloggs.png` exists, it'll return `joebloggs-1.png`. A subsequent call will return `joebloggs-2.png`, and so on.

The method returns the filename only, but you can get the full path with `fullPathToFile()`.

For example:

```
$directory = new Directory( '/path/to/avatars' );
$filename = $directory-> ensureUniqueFilename( 'joebloggs.png' );
// joebloggs-1.png
$filepath = $directory->fullPathToFile( $filename );
// /path/to/avatars/joebloggs-1.png
```

Creating a File
---------------

[](#creating-a-file)

To create an empty file in a directory:

```
$directory->createFile( 'filename.txt' );
```

To create a file, providing its contents:

```
$directory->createFile( 'filename.txt', 'the contents' );
```

Copying a File Into a Directory
-------------------------------

[](#copying-a-file-into-a-directory)

To copy a file into the directory:

```
$directory->copyFileInto( '/path/to/your/file' );
```

Deleting a Directory
--------------------

[](#deleting-a-directory)

> Use with caution!

To delete a directory, its contents and it's sub-directories, simply call `delete()`.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~937 days

Total

2

Last Release

1848d ago

Major Versions

0.0.1 → 1.02021-04-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1bd633ffdf0f66a73a288c4edcacfa43d214cd95a53384c6fe5ea4b3554dd9d?d=identicon)[lukaswhite](/maintainers/lukaswhite)

---

Top Contributors

[![lukaswhite](https://avatars.githubusercontent.com/u/999014?v=4)](https://github.com/lukaswhite "lukaswhite (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lukaswhite-directory/health.svg)

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

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[google/cloud-storage

Cloud Storage Client for PHP

34390.8M125](/packages/google-cloud-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15261.6M2.6k](/packages/illuminate-filesystem)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)

PHPackages © 2026

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