Skip to content
Files Utility

Available Methods

Get the assumed PSR-4 class name for the file. Same as name(): only the last extension is removed, so Rupert.test.php gives Rupert.test.

$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->className();
// RandomClassForTestingWith

If the path is a file and if it exists, appends content to the end of the file

$file = new Utility('./logfile.txt');
echo $file->appendContent('hello world');
// ... hello world

If the path is a file and if it exists, gets the content of the file

$file = new Utility('./logfile.txt');
echo $file->content();
// ...

Delete the file or directory with the given path

$file = new Utility('./src/RandomClassForTestingWith.php');
$file->delete();

Get the directory of the path. A path that exists as a directory is always returned as is, even if its name has a dot (v1.2, .git); for paths that do not exist, an extension means a file.

$file = new Utility('./src/RandomClassForTestingWith.php');
$file->directory(); // ./src
$file = new Utility('./src/App/Commands');
$file->directory(); // ./src/App/Commands

Does the file or directory actually exists

$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->exists(); // true
$file->delete();
echo $file->exists(); // false

Guess the path’s file extension

$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->extension(); // php
// get with dot prefix
echo $file->extension(true); // .php

Get a collection of spl file objects, from within the path if it’s a directory

$file = new Utility('./src/');
echo $file->files(); // [...$srcFiles]

Get the fully qualified class name from the file (if it’s a PHP file)

$file = new Utility('./src/');
echo $file->fullyQualifiedClassname(); // App/RandomClassForTestingWith

Does the current path have an absolute file path

$file = new Utility('/src');
echo $file->hasAbsolutePath(); // true
$file = new Utility('~/src');
echo $file->hasAbsolutePath(); // false

Checks if the path is a directory

$file = new Utility('./src/');
echo $file->isDirectory(); // true
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->isDirectory(); // false

Checks if the path is a file

$file = new Utility('./src/');
echo $file->isFile(); // false
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->isFile(); // true

Create directory if it doesn’t already exist

$file = new Utility('~/Code/app');
echo $file->makeDirectory();

Get the namespace of the file (if it’s a PHP file). The file is tokenised, so braced namespaces, tabs, CRLF line endings and commented-out declarations are handled. Throws FileFormatException when no namespace is declared. The old misspelt FileFormatExpection is deprecated but remains its parent class, so existing catch blocks keep working.

$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->namespace(); // App

Stream the file one line at a time without loading it all into memory. Line endings are stripped. Throws FileNotFoundException straight away if the file does not exist.

$file = new Utility('./logfile.txt');
foreach ($file->lines() as $line) {
echo $line;
}

Get the file or directory name, optionally with its extension

$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->name(); // RandomClassForTestingWith
// get with file extension
echo $file->name(true); // RandomClassForTestingWith.php

Get the path of the file

$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->path(); // ./src/RandomClassForTestingWith.php

If the path is a file and if it exists, sets the content of the file.

The write is atomic: content goes to a temporary file in the same directory, which is then renamed over the original, so readers never see a partial file. Permissions are preserved and symlinks are written through. appendContent writes in place under an exclusive lock.

$file = new Utility('./logfile.txt');
echo $file->setContent('hello world');
// hello world

Touch a file to, ensure it exists

$file = new Utility('~/Code/app.php');
echo $file->touchFile();

Touch a file or directory, to ensure it exists.

If the path doesn’t exist and has no file extension, a directory is assumed to be wanted. A path that already exists is left untouched.

$file = new Utility('~/Code/app.php');
echo $file->touch(); // file created
$file = new Utility('~/Code/app');
echo $file->touch(); // directory created
  • Pass a $root as the second constructor argument (new Utility($path, $root)) to confine reads (content, namespace, lines, files) and mutating operations (delete, setContent, appendContent, touch, touchFile, makeDirectory). Paths that resolve outside $root are refused. Without a $root, no confinement applies, so never build paths from untrusted input without one.
  • Stream wrapper paths such as phar://, php://, data: and http:// are rejected with an InvalidArgumentException. Only plain paths and file: are accepted.