classes/File.md

File

Overview:

File provides a mount-based filesystem abstraction. Filesystems are mounted by alias and accessed with alias://path URIs.

Use File when your app must read and write across local, memory, or archive-backed filesystems behind a single mount-based path format.

Key behavior:

Public API:

Example:

File::mount('local', 'local', ['root' => '/var/data']);
File::write('local://report.txt', 'hello');

The File module expose utilities for working with virtual filesystems.

Mounting Virtual Filesystems

---

You can register a Virtual Filesystem (VFS) handler via the mount method.

File::mount($alias, $driver, $options = null)

The simplest VFS is memory, a non-persistent temporary filesystem.

// Mount memory VFS to 'mem' handle
File::mount('mem','memory');

If a VFS needs some options for setup you can pass them via a dictionary.

For example, the native VFS, who maps an OS Filesystem path to an handle, needs the base path to be used as its root.

// Mount temp directory to 'tmp' handle
File::mount('tmp','native', [
    'root' => sys_get_temp_dir(),
]);

// Mount ./app/uploads directory to 'uploads' handle
File::mount('assets','native', [
    'root' => __DIR__.'/app/assets',
]);

Virtual Filesystems

---

Memory

The Memory VFS is a volatile (non persistent) read/write temporary filesystem.

Native

The Native VFS is proxy for the OS provided filesystem.

OptionDescriptionDefaultMandatory
rootThe local filesystem root absolute path/NO

ZIP

The ZIP VFS permits read/write access to the files inside a ZIP archive.

OptionDescriptionDefaultMandatory
rootThe absolute path of the ZIP archiveTEMP-ZIP-ARCHIVENO

File Operations

---

Check if a file exists : File::exists($path)

if (! File::exists("assets://images/logo.png") ) echo "File not found.";

Read a file : File::read($path)

$content = File::read("assets://images/logo.png");

Write a file : File::write($path, $data)

File::write("assets://text/lorem.txt","Lorem Ipsum Dolor Sit Amet");

Append to a file : File::append($path, $data)

File::append("assets://foobar.txt","Foo");
File::append("assets://foobar.txt","Bar");

echo File::read("assets://foobar.txt");
// FooBar

Delete a file : File::delete($path)

File::delete("assets://useless_file.txt");

Move/Rename a file : File::move($old_path, $new_path)

File::move("assets://files/old.txt", "assets://files/new.txt");

Search/Locate : File::search($file_glob)

/**
 * We have 2 mounted VFS : ["mem","assets"]
 *  mem has the following tree :
 *    - /test/alpha.txt
 *    - /test/beta.png
 *    - /info.txt
 *    
 *  assets has the following tree :
 *    - /img/1.jpg
 *    - /img/2.jpg
 *    - /info.txt
 */

$texts = File::search("*.txt");

Results:

[ 
  "mem://test/alpha.txt",
  "mem://info.txt"
  "assets://info.txt"
]

If a VFS handle is not provided an implicit search is resolved with the first result.

The priority order is the mount order of the VFS.

Example:

File::mount('mem-1','memory');
File::mount('mem-2','memory');

File::write('mem-2://test.txt',"MEMORY 2");
File::write('mem-1://test.txt',"MEMORY 1");

echo File::read('test.txt');

Results:

MEMORY 1