An intuitive, standalone, and object-oriented library for file and path operations.
Path-PHP aims to allow you to build and manipulate paths, iterate over files and directories, and perform virtually all of PHP's built-in file operations.
It also aims to fill the gaps of missing object-oriented and convenient file manipulation methods in the PHP core, by providing common methods that are absent from the builtin methods, such as a variety of recursive operations, and a real error management during file operations.
Usage
Make a Path object :
<?php
use Path\Path;
$dir = new Path('/home/user/myFile.txt');
Build and manipulate paths :
<?php
use Path\Path;
// Instantiate a new path
$dir = new Path('/home/user/workdir');
// Add a part to your path: '/home/user/workdir/foo.txt'
$newPath = $dir->append('foo.txt');
// Add multiple parts to your path: '/home/user/workdir/sub_dir/foo.txt'
$otherPath = $dir->append('sub_dir', 'foo.txt');
// Get the parent dir of your path: '/home/user'
$parentDir = $dir->parent();
Browse directories and files :
<?php
use Path\Path;
$dir = new Path('/home/user/workdir');
// Iterate over the files in '/home/user/workdir'
foreach ($dir->files() as $file) {
// ...
}
// Iterate over the directories in '/home/user/workdir'
foreach ($dir->dirs() as $dir) {
// ...
}
// Recursively walk over the files and directories with walkDir or glob
foreach ($dir->walkDirs() as $dir) {
// ...
}
foreach ($dir->glob('*.txt') as $file) {
// ...
}
Perform all the file operations allowed by PHP's builtin functions :
<?php
use Path\Path;
$file = new Path('/home/user/file.txt');
// Get file's name (without extension)
$file->name();
// Or file's basename (with extension)
$file->basename();
// Or get its extension
$file->ext();
// Check if the file exists
$file->exists();
// Get its absolute path
$file->absPath();
// And more
$file->chmod(0777);
$file->isLink();
$file->mtime();
// ...etc
And more :
<?php
use Path\Path;
$dir = new Path('/home/user/foo');
// Recursively copy a directory
$target = new Path('/home/user/bar');
$dir->copyTree($target);
// Recursively remove a directory and its content
$dir->rmdir(true);
// Expand global variables in the path
$dir->expandVars();
// And more
Get explicit and catchable exceptions :
<?php
use Path\Path;
$file = new Path('/home/user/foo.txt');
$file->remove();
// >>> FileNotFoundException('File does not exist: /home/user/foo.txt')