Filesystem
The Filesystem is a lightweight utility for common file and directory operations, including creating, reading, writing, copying, moving, and removing files. It provides a consistent API that keeps filesystem handling simple, reusable, and readable.
One of the key features of the SigmaPHP Framework is its dependency injection (DI) system. Services can be injected into controllers and other classes through their constructors, allowing the framework to manage dependencies without requiring classes to instantiate them manually.
The following example demonstrates constructor-based dependency injection:
<?php
namespace App\Controllers;
use SigmaPHP\Filesystem\Filesystem;
class UserController
{
protected $filesystem;
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function index()
{
$this->filesystem->read('/path/to/file');
}
}
Also, Filesystem class can be instantiated directly whenever
filesystem operations are required.
<?php
$filesystem = new Filesystem();
Once the filesystem instance has been created, it can be used to perform common file and directory operations.
For example, create a file and write content to it:
<?php
$filesystem = new Filesystem();
$filesystem->create(
'/var/www/project/example.txt',
0644
);
$filesystem->write(
'/var/www/project/example.txt',
'Hello, SigmaPHP!'
);
The same instance can then be used to read the file:
<?php
$content = $filesystem->read(
'/var/www/project/example.txt',
0,
1000
);
echo $content;
Available Methods
list()
Lists files within a directory and its subdirectories. Options allow you to include full paths, control whether file extensions are included, and return files only.
$files = $filesystem->list(
'/var/www/project',
true,
true,
true
);
Returns an array of file paths or file names, depending on the supplied options.
exists()
Checks whether a file or directory exists at the specified path.
if ($filesystem->exists('/var/www/project/config.php')) {
echo 'File exists.';
}
Returns true if the path exists; otherwise, returns false.
dirHas()
Checks whether a directory contains a specific file or subdirectory.
if ($filesystem->dirHas('/var/www/project', 'config.php')) {
echo 'config.php exists.';
}
Returns true if the target exists inside the specified directory.
create()
Creates a new file with the specified permissions.
$filesystem->create(
'/var/www/project/example.txt',
0644
);
Returns true when the file is created successfully.
rename()
Renames a file or directory by changing its path.
$filesystem->rename(
'/var/www/project/old.txt',
'/var/www/project/new.txt'
);
Returns true when the operation succeeds.
copy()
Copies a file to another location.
$filesystem->copy(
'/var/www/project/source.txt',
'/var/www/project/backup/source.txt'
);
Returns true when the file is copied successfully.
move()
Moves a file to another location.
$filesystem->move(
'/var/www/project/source.txt',
'/var/www/project/archive/source.txt'
);
Returns true when the file is moved successfully.
remove()
Removes a file from the filesystem.
$filesystem->remove(
'/var/www/project/example.txt'
);
Returns true when the file is removed successfully.
write()
Writes content to a file. Existing content may be replaced.
$filesystem->write(
'/var/www/project/example.txt',
'Hello, SigmaPHP!'
);
Returns true when the content is written successfully.
append()
Appends content to the end of an existing file.
$filesystem->append(
'/var/www/project/example.txt',
"\nAdditional content."
);
Returns true when the content is appended successfully.
read()
Reads content from a file, starting at the specified offset and up to the requested length.
$content = $filesystem->read(
'/var/www/project/example.txt',
0,
100
);
echo $content;
Returns the file content as a string.
createDir()
Creates a directory with the specified permissions. Parent directories can also be created recursively.
$filesystem->createDir(
'/var/www/project/storage',
0755,
true
);
Returns true when the directory is created successfully.
removeDir()
Removes a directory and its contents.
$filesystem->removeDir(
'/var/www/project/storage/cache'
);
Returns true when the directory and its contents are removed successfully.
isDir()
Checks whether a specified path points to a directory.
$isDirectory = $filesystem->isDir(
'/var/www/project'
);
Returns true if the path is a directory; otherwise, returns false.
symlink()
Creates a symbolic link to a file.
$filesystem->symlink(
'/var/www/project/config.php',
'/var/www/project/config-link.php'
);
Returns true if the symbolic link is created successfully; otherwise, returns false.
concatPath()
Combines multiple path segments into a single path string.
$path = $filesystem->concatPath(
'/var/www/project',
'storage',
'logs',
'app.log'
);
Returns the concatenated path as a string.
addExtension()
Adds an extension to a file name.
$filename = $filesystem->addExtension(
'document',
'pdf'
);
// Result: document.pdf
Returns the file name with the specified extension.
removeExtension()
Removes an extension from a file name. By default, only the last extension is removed. You can specify a custom extension, including compound extensions.
For a file such as script.min.js,
the default behavior removes only .js. To remove the entire
min.js suffix, specify it explicitly.
$filename = $filesystem->removeExtension(
'document.pdf'
);
// Result: document
$filename = $filesystem->removeExtension(
'script.min.js'
);
// Result: script.min
$filename = $filesystem->removeExtension(
'script.min.js',
'min.js'
);
// Result: script
Returns the file name without the specified extension or, if no extension is provided, without its last extension.
getFileName()
Extracts the file name from a path.
$filename = $filesystem->getFileName(
'/var/www/project/config.php'
);
// Result: config.php
Returns the extracted file name as a string.
getPath()
Extracts the directory path from a full file path.
$path = $filesystem->getPath(
'/var/www/project/document.pdf'
);
// Result: /var/www/project
Returns the directory path as a string.
changeMode()
Changes the permissions of a file or directory using an octal permission value.
$filesystem->changeMode(
'/var/www/project/storage',
0755
);
Returns true if the permissions are changed successfully;
otherwise, returns false.
In this example, 0755 grants the owner read, write, and execute
permissions, while group members and other users receive read and execute
permissions.
changeGroup()
Changes the group ownership of a file or directory.
$filesystem->changeGroup(
'/var/www/project/storage',
'www-data'
);
Returns true if the group is changed successfully; otherwise, returns false.
changeOwner()
Changes the owner of a file or directory.
$filesystem->changeOwner(
'/var/www/project/storage',
'www-data'
);
Returns true if the owner is changed successfully; otherwise, returns false.
Changing file permissions, group ownership, or file ownership depends on the operating system and the privileges of the executing user. These operations may fail if the process does not have sufficient permissions.