PHPackages                             azbosakov/filesystem-chroot - 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. azbosakov/filesystem-chroot

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

azbosakov/filesystem-chroot
===========================

Filesystem chroot abstraction - copy, move, delete, list - relative to the defined root dir.

v0.9.3(6y ago)019MITPHP

Since Mar 10Pushed 6y agoCompare

[ Source](https://github.com/AZBosakov/filesystem-chroot)[ Packagist](https://packagist.org/packages/azbosakov/filesystem-chroot)[ RSS](/packages/azbosakov-filesystem-chroot/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (5)Used By (0)

Class description
=================

[](#class-description)

A filesystem chroot. Initialized with an existing directory path as a root dir. Most of the methods operate on paths, relative to the root dir, passed to the constructor.

The class contains functions for path normalization, copy, move, delete, list, etc. The method names and behavior are inspired by the corresponding UNIX commands - `cp`, `mv`, `rm`, `ls`, `rmdir`, `find`.

Method reference
================

[](#method-reference)

### public function \_\_construct(string $rootDir = '/')

[](#public-function-__constructstring-rootdir--)

The `$rootDir` parameter must be an existing directory, relative to the default root directory for the class.

For example:

- class default: "`/a/b/c/d`", `$rootDir`: "`/x/y`" or "`x/y`", the root dir for the object will be "`/a/b/c/d/x/y`".
- class default: "`/a/b/c/d`", `$rootDir`: "`../../x/y`", the root dir for the object will be "`/a/b/x/y`".

### public static function setDefaultRoot(string $rootDir): bool

[](#public-static-function-setdefaultrootstring-rootdir-bool)

Sets the default root directory for the entire class. Initially it is "/" - the filesystem root. Can be changed **only once** - preferably before the first use of the class. Returns **true** on successful change (the first invocation with existing, non-'/' path), **false** otherwise.

Intended use - set the default at the document root dir, then instantiate with something like

`$imgRoot = new ...\Chroot('/images')`

or

`$tplRoot = new ...\Chroot('/templates')`,

etc.

### public static function getDefaultRoot(): string

[](#public-static-function-getdefaultroot-string)

Returns the default root directory for the class.

### public function getRootDir(): string

[](#public-function-getrootdir-string)

Returns the root directory for the particular instance.

### public function getUmask(): int

[](#public-function-getumask-int)

Returns the **umask**, used for `mkdir()` mode parameter. The mode is calculated as `0777 & ~umask`.

### public function setUmask(int $umask): self

[](#public-function-setumaskint-umask-self)

Sets the umask. Returns the instance.

### public static function normalizePath(string $path, string $relTo = '/'): ?string

[](#public-static-function-normalizepathstring-path-string-relto---string)

Normalizes the `$path` to absolute path. If `$path` does not start with '/', it's taken as a relative to the `$relTo` path.

The path does not need to be existing.

If the path can't be normalized (too many '..'), returns **null**.

### public function realpath(string $sitePath): ?string

[](#public-function-realpathstring-sitepath-string)

Normalizes the `$sitePath` to absolute path, starting from the instance root. If `$sitePath` does not start with '/', it's taken as a relative to the instance's Current Working Directory (CWD).

If the path can't be normalized (too many '..'), returns **null**.

### public function \_\_invoke(string $sitePath): ?string

[](#public-function-__invokestring-sitepath-string)

A shorthand for `->syspath()`, to save typing.

### public function syspath(string $sitePath): ?string

[](#public-function-syspathstring-sitepath-string)

Maps a path, relative to the instance, to a filesystem path. Eg.:

instance root: "`/a/b/c`", instance path: "`/x/y`" -&gt; "`/a/b/c/x/y`"

If the `$sitePath` can't be normalized as local one, returns **null**.

### public function \_\_toString()

[](#public-function-__tostring)

Returns the instance root concatenated with the instance CWD, so the object can be used in a string interpolation, like:

```
...::setDefaultRoot('/srv/doc/root');
...
$imgRoot = new ...\Chroot('/images');   // root = '/srv/doc/root/images'
$imgRoot->cd('big');                    // CWD = '/big'; (string)$imgRoot == '/srv/doc/root/images/big'
$imgFile = 'xxx.jpg';
doSomething("$imgRoot/$imgFile");       // "$imgRoot/$imgFile" == '/srv/doc/root/images/big/xxx.jpg'

```

### public function sitepath(string $sysPath): ?string

[](#public-function-sitepathstring-syspath-string)

The opposite of `->syspath(...)`. Maps a filesystem path to a local one, as long as the system one is inside the instance root. Returns **null** otherwise.

Eg.: root: "`/a/b/c`", `$sysPath`: "`/a/b/c/d/e`" -&gt; "`/d/e`"

### public function isFile(string $sitePath): bool

[](#public-function-isfilestring-sitepath-bool)

Checks if `$sitePath` is a file.

### public function isDir(string $sitePath): bool

[](#public-function-isdirstring-sitepath-bool)

Checks if `$sitePath` is a directory.

### public function cd(string $sitePath): bool

[](#public-function-cdstring-sitepath-bool)

Changes the instance's CWD. Returns **true** on success, **false** if the `$sitePath` can't be normalized.

### public function pwd(): string

[](#public-function-pwd-string)

Returns the instance's CWD.

### public function ls(string $glob = '\*'): array

[](#public-function-lsstring-glob---array)

Returns a list of the paths, matching the `$glob`, relative to CWD.

### public function find(string $glob = '\*', string $dir = '.'): array

[](#public-function-findstring-glob---string-dir---array)

A recursive `->ls(...)`, relative to the CWD or `$dir`.

### public function cp(string $siteSrc, string $siteDst, bool $overwrite = false): bool

[](#public-function-cpstring-sitesrc-string-sitedst-bool-overwrite--false-bool)

Copy a file/directory. If `$siteDst` ends with '/', it is taken as the `dirname(...)` of the destination, and the `basename(...)` is the `basename(...)` of the source. Eg.:

- SRC: "`/dir111/file111`", DST: "`/dir222/file222`" -&gt; "`/dir111/file111`" is copied as "`/dir222/file222`"
- SRC: "`/dir111/file111`", DST: "`/dir222/`" -&gt; "`/dir111/file111`" is copied as "`/dir222/file111`"

If the destination exists, the copy fails, unless `$overwrite` is true.

### public function mv(string $siteSrc, string $siteDst, bool $overwrite = false): bool

[](#public-function-mvstring-sitesrc-string-sitedst-bool-overwrite--false-bool)

Move a file/directory. If `$siteDst` ends with '/', it is taken as the `dirname(...)` of the destination, and the `basename(...)` is the `basename(...)` of the source. Eg.:

- SRC: "`/dir111/file111`", DST: "`/dir222/file222`" -&gt; "`/dir111/file111`" is moved as "`/dir222/file222`"
- SRC: "`/dir111/file111`", DST: "`/dir222/`" -&gt; "`/dir111/file111`" is moved as "`/dir222/file111`"

If the destination exists, the move fails, unless `$overwrite` is true.

### public function rm(string $sitePath, bool $rf = false): bool

[](#public-function-rmstring-sitepath-bool-rf--false-bool)

Delete a path. If `$rf` is **true**, delete directories recursively, like the UNIX's `rm -rf ...`. Returns **true** on success, **false** otherwise.

### public function mkdir(string $sitePath, bool $mkpath = false): bool

[](#public-function-mkdirstring-sitepath-bool-mkpath--false-bool)

Creates a subdirectory. If `$mkpath` is true, can create multiple levels, like the UNIX's `mkdir -p ...`.

### public function rmdir(string $sitePath, bool $recursive = false): bool

[](#public-function-rmdirstring-sitepath-bool-recursive--false-bool)

Removes a directory. Fails if directory is non-empty, unless `$recursive` is **true**, for recursive removal. With `$recursive`: **true**, acts like `->rm(...)` with `$rf`: **true**.

### public static function rCopy(string $fsSrc, string $fsDst, bool $overwrite = false): bool

[](#public-static-function-rcopystring-fssrc-string-fsdst-bool-overwrite--false-bool)

Recursive copy. The arguments **filesystem** paths, **not** local ones.

### public static function rRemove(string $fsPath): bool

[](#public-static-function-rremovestring-fspath-bool)

Recursive delete. The arguments **filesystem** paths, **not** local ones.

### public static function rGlob(string $pattern = '\*', string $dir = '.'): array

[](#public-static-function-rglobstring-pattern---string-dir---array)

Recursive `glob()`. The arguments **filesystem** paths, **not** local ones.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

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

Total

4

Last Release

2531d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9fe6053e1652840db4fa7c442c597345a10f0ea26276bfa2e732bbb32b7c519d?d=identicon)[azbosakov](/maintainers/azbosakov)

---

Top Contributors

[![AZBosakov](https://avatars.githubusercontent.com/u/44407115?v=4)](https://github.com/AZBosakov "AZBosakov (2 commits)")

---

Tags

filesystemcopylistdeletemovechroot

### Embed Badge

![Health badge](/badges/azbosakov-filesystem-chroot/health.svg)

```
[![Health](https://phpackages.com/badges/azbosakov-filesystem-chroot/health.svg)](https://phpackages.com/packages/azbosakov-filesystem-chroot)
```

###  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-bundle

Symfony bundle integrating Flysystem into Symfony applications

40029.5M87](/packages/league-flysystem-bundle)

PHPackages © 2026

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