PHPackages                             code-distortion/path - 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. code-distortion/path

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

code-distortion/path
====================

A package that lets you deal with paths in a normalised way

0.1.1(1y ago)017MITPHPPHP 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\* | 8.4.\*CI passing

Since Dec 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/code-distortion/path)[ Packagist](https://packagist.org/packages/code-distortion/path)[ Docs](https://github.com/code-distortion/path)[ RSS](/packages/code-distortion-path/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

Path
====

[](#path)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7258fc78f15c7989c9882b98ed2c1310694aca45c779186f3b596dfb3f7ef3cf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f64652d646973746f7274696f6e2f706174682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/code-distortion/path)[![PHP Version](https://camo.githubusercontent.com/17c04f775ccf1006bd98aaeda77764eb79a06963000af1655a02b85e1d8b77b2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e30253230746f253230382e342d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/17c04f775ccf1006bd98aaeda77764eb79a06963000af1655a02b85e1d8b77b2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e30253230746f253230382e342d626c75653f7374796c653d666c61742d737175617265)[![GitHub Workflow Status](https://camo.githubusercontent.com/93f1ef7f5ffa20d0506bfef4bb3df73c16bd7d0b41f8fbab7865a1cb644d663d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f64652d646973746f7274696f6e2f706174682f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/code-distortion/path/actions)[![Buy The World a Tree](https://camo.githubusercontent.com/dc3f77a9b22c3bc83c7b7d863bf138a7ca3418f1826b0b16d073d0aa87c16bc4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74726565776172652d2546302539462538432542332d6c69676874677265656e3f7374796c653d666c61742d737175617265)](https://plant.treeware.earth/code-distortion/path)[![Contributor Covenant](https://camo.githubusercontent.com/902d296a65b2997bada7e7717fd929d9177f3bd95414cbb5ea2ed843c680f314/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6e7472696275746f72253230636f76656e616e742d76322e3125323061646f707465642d6666363962342e7376673f7374796c653d666c61742d737175617265)](.github/CODE_OF_CONDUCT.md)

***code-distortion/path*** is a package that lets you deal with paths in a normalised way.

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

[](#installation)

Install the package via composer:

```
composer require code-distortion/path
```

Usage
-----

[](#usage)

```
Use CodeDistortion\Path\Path;

$path = Path::new('/path/to/file.txt');

(string) $path;             // '/path/to/file.txt' (castable to a string)
$path->getDir();            // '/path/to/' (the dir as a new Path object)
$path->getFilename();       // 'file.txt'
$path->getFilename(false);  // 'file'
$path->getExtension();      // '.txt'
$path->getExtension(false); // 'txt'
$path->isAbsolute();        // true
$path->isRelative();        // false
```

> ***Note:*** This package is designed to work with paths as strings in *memory*.
>
> It ***doesn't deal with actual directories or files*** in any way. It doesn't care what's in the filesystem.

> ***Note:*** This package takes the opinion that paths for directories end with a trailing slash, and paths for files do not.

If you want to make sure the input you're dealing with starts off specifically as a directory, or a file, you can specify this when creating the object.

```
// dir paths
Path::new('/path/to/thing/');   // '/path/to/thing/'
Path::newDir('/path/to/thing'); // '/path/to/thing/' (enforces that it's a dir)
// file paths
Path::new('/path/to/thing');      // '/path/to/thing'
Path::newFile('/path/to/thing/'); // '/path/to/thing' (enforces that it's a file)
```

You can remove unnecessary parts from a path.

```
Path::new('/a//b/.././c')->resolve(); // '/a/c' - removes unnecessary parts
```

You can add paths together, using one as the base.

```
Path::newDir('/path/to/uploads/')->add('my-file.txt');           // '/path/to/uploads/my-file.txt'
// make sure that your file doesn't break out from the base
Path::newDir('/path/to/uploads/')->add('../my-file.txt');        // '/path/to/uploads/my-file.txt'
// or allow it to break out
Path::newDir('/path/to/uploads/')->add('../my-file.txt', false); // '/path/to/my-file.txt'
```

Path will normalise forward and backslashes in the input, generating output in your native OS format (but the output separator can be overridden).

```
Path::newDir('\\path\\to\\file.txt');               // '/path/to/file.txt' (on a *nix OS)
Path::newDir('/path/to/file.txt');                  // '/path/to/file.txt' (on a *nix OS)
Path::newDir('/path/to/file.txt')->separator('\\'); // '\path\to\file.txt'
```

You can clone a Path object.

```
$pathA = Path::new('/path/to/file.txt');
$pathB = $pathA->copy();
$pathB !== $pathA; // true
```

The `PathImmutable` class is also available. Each change to a `PathImmutable` object returns a new instance. Both `Path` and `PathImmutable` implement `CodeDistortion\Path\PathInterface`.

```
$pathA = PathImmutable::newDir('/path/to/');
$pathB = $pathA->add('file.txt');
$pathA !== $pathB; // true
```

Testing This Package
--------------------

[](#testing-this-package)

- Clone this package: `git clone https://github.com/code-distortion/path.git .`
- Run `composer install` to install dependencies
- Run the tests: `composer test`

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

### SemVer

[](#semver)

This library uses [SemVer 2.0.0](https://semver.org/) versioning. This means that changes to `X` indicate a breaking change: `0.0.X`, `0.X.y`, `X.y.z`. When this library changes to version 1.0.0, 2.0.0 and so forth, it doesn't indicate that it's necessarily a notable release, it simply indicates that the changes were breaking.

Treeware
--------

[](#treeware)

This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/code-distortion/path) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

### Code of Conduct

[](#code-of-conduct)

Please see [CODE\_OF\_CONDUCT](.github/CODE_OF_CONDUCT.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Tim Chandler](https://github.com/code-distortion)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance42

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 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 ~397 days

Total

2

Last Release

472d ago

PHP version history (2 changes)0.1.0PHP 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\*

0.1.1PHP 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\* | 8.4.\*

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/56794290?v=4)[Tim](/maintainers/code-distortion)[@code-distortion](https://github.com/code-distortion)

---

Top Contributors

[![code-distortion](https://avatars.githubusercontent.com/u/56794290?v=4)](https://github.com/code-distortion "code-distortion (2 commits)")

---

Tags

pathfiledirectorydirfilename

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/code-distortion-path/health.svg)

```
[![Health](https://phpackages.com/badges/code-distortion-path/health.svg)](https://phpackages.com/packages/code-distortion-path)
```

###  Alternatives

[adlawson/vfs

Virtual file system

300433.0k21](/packages/adlawson-vfs)[wikimedia/relpath

Work with file paths to join or find the relative path

11894.3k2](/packages/wikimedia-relpath)[riimu/kit-pathjoin

Cross-platform library for normalizing and joining file system paths

12603.0k4](/packages/riimu-kit-pathjoin)[icecave/temptation

Painless temporary files and directories that clean up after themselves.

129.3k2](/packages/icecave-temptation)[crysalead/dir

Recursive directory scanner to locate directories and/or files in a file system

13214.5k4](/packages/crysalead-dir)[internal/path

Type-safe, immutable file path library with cross-platform support and automatic normalization.

1137.7k3](/packages/internal-path)

PHPackages © 2026

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