Edit on GitHub
Jump to docs navigation

Extending / Filesystem Layer / Filesystems

Note: You are currently reading the documentation for Bolt 3.7. Looking for the documentation for Bolt 5.2 instead?

The examples in this section assume you have a $filesystem variable set via the filesystem manager.

For guidance see the Getting a mount point's filesystem object section of the Filesystem Manager page.

Using filesystem objects

To work with filesystem objects, such as files & directories, it is highly encouraged that you read the following two sections.

Checking a filesystem object exists

To check if a filesystem has an existing file or directory, you can use the $filesystem->has($path) method. It will return a boolean value, true if the subject exists, false otherwise.

For example, to check if a file exists prior to fetching it with ->get():

    $path = 'relative/path/header.jpg';
    if ($filesystem->has($path)) {
        $object = $filesystem->get($path);
    }

Generic getter

To get a file or directory you can use $filesystem->get($path) to get an PHP object that represents an existing filesystem object.

    $filesystem = $manager->getFilesystem('files');
    $relativePathToFile = 'programming/lecture-notes/oop.txt';

    // Attempt to get the file `files://programming/lecture-notes/oop.txt`
    $object = $filesystem->get($relativePathToFile);

Note: $filesystem->get($path) expects the file or directory to exist at the relative path variable's location. If this file does not exist, then the filesystem manager will throw an exception of \Bolt\Filesystem\Exception\FileNotFoundException` type.



Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.