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.
To include the FS object in a route, use this:
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
to set the base directory to the directory the file you're initialising Leaf FS is in.
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.
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 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.
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;
appendFile is almost exactly the same as writeFile, except that instead of replacing the contant in a file, it adds to it.
readFile returns the data found in a file. It takes 1 parameter: the file name/path+file name.
renameFile renames a file. It takes 2 parameter: the file name/path+file name to rename and it's new name.
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.
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.
Next StepsSimple Routing
Simple Form Validation