This package seeks to make your filesystem operations easier by bringing in some easy-to-use verbal methods.
Run composer require anteris-dev/file-explorer
Requirements
- PHP ^7.4 for stricter type casting.
- Spatie Data-Transfer-Objects for file objects.
- Symfony Filesystem for their existing easy-to-use functions.
To get started with this package, create a new instance of the FileExplorer
class. If you pass a directory to the constructor, this will be your starting location, otherwise the current working directory is used.
Example:
use Anteris\FileExplorer\FileExplorer;
$fileExplorer = new FileExplorer; // This uses the cwd
$fileExplorer = new FileExplorer('/users/foo'); // This uses /users/foo
There are several methods that will help you to start interacting with the file system. These are listed below.
This method creates a new directory. If a relative path, this directory will be created relative to the current pointer. If absolute, it will be created at that location.
Example:
$fileExplorer->createDirectory('./myFolder'); // Creates the folder here
$fileExplorer->createDirectory('/users/foo/myFolder'); // Creates the folder in /users/foo
Creates a new directory and sets the current context to that directory.
Example:
use Anteris\FileExplorer\FileExplorer;
$fileExplorer = new FileExplorer('/users/foo');
$fileExplorer->createAndEnterDirectory('testing');
echo $fileExplorer->getCurrentDirectory(); // Returns '/users/foo/testing/'
This method creates a new file. If a relative path, this file will be created relative to the current pointer. If absolute, it will be created at that location. Unless $overwrite is passed as true, the file will not be overwritten.
Example:
$fileExplorer->createFile('test.txt', 'Hello world!'); // Will not overwrite test.txt
$fileExplorer->createFile('test.txt', 'Hello world!', true); // Will overwrite test.txt
Sets the current context of the class to this directory (think about entering a sub-folder within your file browser). If a relative path is passed, this is relative to the current directory context.
Example:
$fileExplorer->enterDirectory('mySubFolder'); // Relative directory
$fileExplorer->enterDirectory('/users/foo'); // Absolute path
Returns true if the requested resource exists, otherwise false. This could be a file or directory. If the path passed is relative, this will be relative to the current directory context.
Example:
$fileExplorer->exists('myFolder');
Returns the current directory context of the class. This is where relative paths are resolved.
Returns a collection of files and directories in the current directory context.
Example:
use Anteris\FileExplorer\FileObject\Directory;
use Anteris\FileExplorer\FileObject\File;
$items = $fileExplorer->getDirectoryContents();
foreach ($items as $item) {
if ($item instanceof Directory) {
echo 'Directory!' . PHP_EOL;
}
if ($item instanceof File) {
echo 'File!' . PHP_EOL;
}
echo $file->name . PHP_EOL;
}
Sets the directory context to the parent folder.
Example:
use Anteris\FileExplorer\FileExplorer;
$fileExplorer = new FileExplorer('/users/foo');
$fileExplorer->goUp();
echo $fileExplorer->getCurrentDirectory(); // returns /users/
Returns true if the path is absolute, otherwise false.
Joins multiple directory paths together. The end is suffixed with a forward slash, so this should not be used with filenames.
Example:
/**
* Returns /users/foo/desktop/
*/
$path = $fileExplorer->joinPaths('/users', '/foo', 'Desktop');