Leaf FS
Basic filesystem operations with Leaf


This is a simple functionality inspired by node js' FileSystem(fs) module. Leaf FS aims to make directory and file management much simpler than what you're currently used to, so as to speed up and ease the dev process.πŸ˜ŠπŸ‘ŒπŸ˜Ž

Leaf FS allows you to read/write to file, create, rename, copy and paste files/directories all with just a few lines of code. All this is performed while maintaining Leaf's simplicity.

Including FS

To include the FS object in a route, use this:

use Leaf\Core\FS; $fs = new FS(__DIR__); $leaf->post('/books/add', function() use($fs) { $book = [ "title" => "something.epub", "content" => "something" ]; $fs->mkDir("books"); $fs->setBaseDirectory("books"); $fs->createFile($book["title"]); $fs->writeFile($book["title"], $book["content"]); $fs->readFile($book["title"]); });
This is a simple example where we create a folder, set our working directory to the new folder, then we create a file in the new folder and add some data to that new file.

Before we move on, keep in mind that the base directory in Leaf FS refers to the current working directory, changing the base directory is just like using cd in a terminal.

Also, when initialising leaf FS, don't forget to pass in the base directory, you can use __DIR__ or "./" to set the base directory to the directory the file you're initialising Leaf FS is in.

$fs = new Leaf\Core\FS("./");

FS Directory Methods

Setting the Base Directory

Setting the base directory will change the folder you're working in...FS methods to read files and more will search the new base directory.


Returning the Base Directory

In case you need to find the current base directory.



This will create a new directory in the current directory, not in the base directory

Let's take this directory structure below, we initialise Leaf FS in our index.php file.

>> index.php // set the base directory to logs $fs->setBaseDirectory("logs"); // create folder $fs->mkDir("new_logs");

After running this code, this is our new directory structure

mkDir uses the current dir that index.php file is in

You can also pass a path to mkDir



mkDirInBase is exactly the same as mkDir, except that mkDirInBase creates a new folder in the base directory.



renameDir is used to rename a directory. It takes in two parameters: the directory you want to rename and it's new name/path.

$fs->renameDir("new", "items"); $fs->renameDir("home/new", "home/items");

FS File Methods


createFile is used to create a new file in the base directory. It takes in the filename or path+filename. If the file already exists, it gives the new file a different name.

$fs->createFile("items.txt"); $fs->createFile("home/items.txt");


writeFile is used to add content to a file, if the file already has content, all the content in there is replaced with the new content. Also, if the file doesn't exist, it will be created and all the content will be added to it. It takes in 2 parameters, the name/path+name of the file and the content;

$fs->writeFile("items.txt", "Hello"); $fs->writeFile("items.txt", [ "name" => "Item 1" ]); $fs->writeFile("items.txt", 1);


appendFile is almost exactly the same as writeFile, except that instead of replacing the contant in a file, it adds to it.

$data = $fs->appendFile("items.txt", "Item name");


readFile returns the data found in a file. It takes 1 parameter: the file name/path+file name.

$data = $fs->readFile("./home/items.txt");


renameFile renames a file. It takes 2 parameter: the file name/path+file name to rename and it's new name.

$data = $fs->renameFile("./home/items.txt", "home/products.txt");


copyFile copies a file from the base directory to another directory. It takes in 3 parameters: the filename, the new path + filename and whether to rename the file if it exists in the new directory. The 3rd parameter is optional, if nothing is passed for the 3rd parameter, it will rename the file. Pass in true to rename the file if it already exists, false to override the file content.

$data = $fs->copyFile("items.txt", "./home/"); $data = $fs->copyFile("items.txt", "./home/", false);


copyToFile also copies a file from the base directory to another directory, but unlike copyFile, copyToFile includes the filename, and it takes in 2 parameters: the filename and the path+filename to rename to.

$data = $fs->copyFile("items.txt", "./home/products.txt");

Next Steps

Simple Routing
Simple Form Validation
Simple Authentication
Database Config