Skip to content

Latest commit

 

History

History
153 lines (110 loc) · 4.61 KB

File metadata and controls

153 lines (110 loc) · 4.61 KB

File Storage Documentation

The FileStorage class offers a simple and efficient way to handle file storage operations within a filesystem. This class is designed to be intuitive, providing functionalities such as creating directories, storing, retrieving, and deleting files.

All methods available in the FileStorage class

  • constructor() Constructs a new FileStorage instance.
  • dir() Create or set the directory context for file operations.
  • get() Retrieves the content of a file at the specified path.
  • put() Stores content in a file at the specified path.
  • append() Appends content to a file at the specified path.
  • prepend() Prepends content to a file at the specified path.
  • delete() Deletes a file from the specified path.
  • allFiles() Retrieves all files in the current or a specified directory.
  • lastModified() Gets the last modification time of a specified file.

Constructing a FileStorage Instance

To start using the FileStorage, instantiate it with the path of the directory where files should be stored.

$storage = new FileStorage('/path/to/storage');

dir()

Create or set the directory context for file operations. If the directory does not exist, it will be created.

// Return a new instance of FileStorage with the new set path.
$storage->dir('user/images');

get()

Retrieves the content of a file at the specified path.

// Get the content of 'report.txt'

$content = $storage->get('report.txt');

Get the content of a file located in a sub directory

// Get the content of a file inside a nested directory
$contentInSubDir = $storage->dir('someDir/anotherDir')->get('report.txt');
// Or shorthand without setting the dir context
$contentInSubDirDirect = $storage->get('someDir/anotherDir/report.txt');

put()

Stores content in a file at the specified path. If the file does not exist, it will be created.

// Store 'Hello, world!' in 'greetings.txt' 
$storage->put('greetings.txt', 'Hello, world!');

Store files in sub directory of the FileStorage

// Store greetings in a sub directory
$storage->dir('someDir/anotherDir')->put('greetings.txt', 'Hello, world!');
// Or shorthand without setting the dir context
$storage->put('someDir/anotherDir/greetings.txt', 'Hello, world!');

append()

Appends content to a file at the specified path. If the file does not exist, it will be created.

// Append 'Hello, world!' to 'greetings.txt'
$storage->append('greetings.txt', 'Hello, world!');

Append to a file located in a sub directory

// Append to a file inside a nested directory
$storage->dir('someDir/anotherDir')->append('greetings.txt', 'Hello, world!');
// Or shorthand without setting the dir context
$storage->append('someDir/anotherDir/greetings.txt', 'Hello, world!');

prepend()

Prepends content to a file at the specified path. If the file does not exist, it will be created.

// Prepend 'Hello, world!' to 'greetings.txt'
$storage->prepend('greetings.txt', 'Hello, world!');

delete()

Deletes a file from the specified path.

// Delete 'oldfile.txt' from storage 
$storage->delete('oldfile.txt');

Delete a file located in a sub directory

// Delete a file inside a nested directory
$storage->dir('someDir/anotherDir')->delete('oldfile.txt');
// Or shorthand without setting the dir context
$storage->delete('someDir/anotherDir/oldfile.txt');

allFiles()

Retrieves the path of all files in the current or a specified directory.

// Get all files in the storage root
$files = $storage->allFiles();

// Get all files in the 'documents' directory
$filesInDocuments = $storage->allFiles('documents');

// Get all files in a subdirectory using dir()
$filesInSubDir = $storage->dir('someDir/anotherDir')->allFiles();

// Alternatively, without setting the dir context
$filesInSubDirDirect = $storage->allFiles('someDir/anotherDir');

lastModified()

Gets the last modification time of a specified file.

// Get the last modification time of 'report.txt'
$lastModified = $storage->lastModified('report.txt');

// Get the last modification time of a file in a subdirectory
$lastModifiedInSubDir = $storage->lastModified('someDir/anotherDir/report.txt');

// Using dir() to set the context before getting the last modification time
$lastModifiedUsingDir = $storage->dir('someDir/anotherDir')->lastModified('report.txt');

mimeType()

Gets the mime type of a specified file.

// Get the mime type of 'report.txt'
$mimeType = $storage->mimeType('report.txt');

// output: text/plain