Skip to content

FileSystem_LocalFile

StefansArya edited this page Apr 18, 2019 · 2 revisions

You may need to assign the namespace to the top of your code

use \Scarlets\Library\FileSystem\LocalFile;

The filesystem configuration are stored on /config/filesystem.php.
Then you can pass {the storage name}/your/path to the LocalFile function.

contents

Get contents of a directory as an array.

LocalFile::contents($path);

# Example
$data = LocalFile::contents('{app}/data');

load

Get content of a file.

LocalFile::load($path);

# Example
$data = LocalFile::load('{app}/data/text.inf');

mkdir

Create directory for $path recursively.

LocalFile::mkdir($path);

size

Calculate size of file or directory and return bytes as integer.

LocalFile::size($path);

append

Append data to the end of file.

LocalFile::append($path, $value);

prepend

Prepend data at the beginning of file.

LocalFile::prepend($path, $value);

put

Put or overwrite file data.

LocalFile::put($path, $value);

search

Search file or folder inside a directory and return as array of string.

LocalFile::search($path, $regex, $recursive = false);

lastModified

Get last modified time of a file.

$timestamp = LocalFile::lastModified($path);

copy

Copy file from path to another path.

LocalFile::copy($path, $to);

move

Move file from path to another path.
This can also being used for renaming file/folder.

LocalFile::move($path, $to);

delete

Delete file or directory specified by $path.
If you only want to remove contents of the directory
you must set $pathRemove to false.

LocalFile::delete($path, $recursive = false, $pathRemove = true);

read

Read file by line ranges, or you can also read by char ranges.

LocalFile::read($path, $ranges, $readChar = false);

# Example
$array = LocalFile::read('{app}/file.txt', [[0, 2], [5, 7]], true);

tail

Tail is used to read lines from bottom of a file.

LocalFile::tail($path, $lines = 1);

zipDirectory

This feature will need zip extension to be installed and turned on from php.ini.
The $regex parameter is similar with LocalFile::search.

LocalFile::zipDirectory($sourcePath, $outZipPath, $password = '', $regex = '');

extractZip

Extract zip file to an directory.

LocalFile::extractZip($zipPath, $to, $password = '');

zipStatus

Check zip status if it has an error.
When check was failed this will return string or error number of ZipArchive.
But if the check was success this will return true, so make sure you used strict equal $msg === true to check if there are no error.

$msg = LocalFile::zipStatus($path);