RdbAdmin Module

FileSystem
in package

Works about files and folders.

Tags
since
0.1

Table of Contents

Properties

$trackCopy  : array<string|int, mixed>
$trackCreate  : array<string|int, mixed>
$trackDeleted  : array<string|int, mixed>
$root  : string

Methods

__construct()  : mixed
Class constructor.
copyFolderRecursive()  : bool
Copy folder recursively.
createFile()  : mixed
Create new file.
createFolder()  : bool
Create folder.
deleteFile()  : bool
Delete a file.
deleteFolder()  : bool
Delete folder and its content.
getBase64File()  : string
Get base 64 string from file name.
getFileExtensionOnly()  : string
Get file extension only.
getFileNameExtension()  : string
Get file name with extension. No path included.
getFileNameOnly()  : string
Get file name only.
getFolderSize()  : int
Get total folder size.
getFullPathWithRoot()  : string
Connect the specify path with root on constructor and get its full path.
getTimestamp()  : int|false
Get file or folder timestamp.
isDir()  : bool
Check for is file exists and it is folder (or directory).
isFile()  : bool
Check for is file exists and it is file.
listFiles()  : array<string|int, mixed>
List all files only, folders only, files and folders in single level of specified $dirname.
listFilesSubFolders()  : array<string|int, mixed>
List all files and subfolders in specified $dirname.
rename()  : bool
Rename a file or directory.
setWebSafeFileName()  : string
Set web safe file name.
writeFile()  : mixed
Write file. (Create a file).
removeUpperPath()  : string
Remove any upper path string such as ../ from string.

Properties

$trackCopy

public array<string|int, mixed> $trackCopy = []

The associative array to track result of copy. Example: [0 => ['src' => 'path1/file.txt', 'dest' => 'path2/file.txt', 'result' => true], 1 => ...]

$trackCreate

public array<string|int, mixed> $trackCreate = []

The associative array to track result of create folder and file. Example: [0 => ['path' => 'path1/file.txt', 'result' => true], 1 => ...]

$trackDeleted

public array<string|int, mixed> $trackDeleted = []

For track deleted files and folders.

$root

protected string $root

Path to root folder that this class will be working with. No trailing slash.

Methods

__construct()

Class constructor.

public __construct([string $root = '' ]) : mixed
Parameters
$root : string = ''

Path to root folder that this class will be working with. If leave this empty and STORAGE_PATH constant was defined, it will be use this constant by default. No trailing slash.

copyFolderRecursive()

Copy folder recursively.

public copyFolderRecursive(string $source, string $dest) : bool

This method does not support overwrite of existing destination file.

Example:

$Fs = new \Rdb\System\Libraries\FileSystem('/www/my-installed-path/public/Modules');
$Fs->copyFolderRecursive('/www/my-installed-path/Modules/MyModule/assets', 'MyModules/assets');
// then every thing in /www/my-installed-path/Modules/MyModule/assets will be copy to /www/my-installed-path/public/Modules/MyModules/assets
Parameters
$source : string

Full path to source folder. No trailing slash.

$dest : string

Path to destination folder inside the $root in class constructor. No trailing slash.

Tags
link

Reference.

Return values
bool

Return true on success, false on failure.

createFile()

Create new file.

public createFile(string $path, string $contents) : mixed

It is an alias of writeFile() method.

Parameters
$path : string

Path to the file to create and write that is not included with $root in the constructor.

$contents : string

The file contents.

Return values
mixed

Returns the number of bytes that were written to the file, or false on failure.

createFolder()

Create folder.

public createFolder(string $dirname) : bool
Parameters
$dirname : string

The folder name that is not included with $root in the constructor. The folder name can included sub folders such as path1/sub1/sub2.

Return values
bool

Return true on success, false on failure. If folder is already exists then it returns true.

deleteFile()

Delete a file.

public deleteFile(string $fileName) : bool
Parameters
$fileName : string

The file name that is not included with $root in the constructor. The file name can included sub folder such as path/to/file.txt

Return values
bool

Return true on success, false on failure. If file is not exists then it returns true.

deleteFolder()

Delete folder and its content.

public deleteFolder(string $dirname[, bool $deleteSelf = false ][, int &$deleted = 0 ][, string $dirnameSelf = null ]) : bool
Parameters
$dirname : string

The folder name that is not included with $root in the constructor. The folder name can included sub folders such as path1/sub1/sub2.

$deleteSelf : bool = false

Delete $dirname itself if set to true, set to false will just delete all files and sub folders in it.

$deleted : int = 0

Dot not set this, it is for count number of deleted items. Do not set this in your calls.

$dirnameSelf : string = null

Do not set this, it is for checking prevent delete self. Do not set this in your calls.

Return values
bool

Return true on success, false on failure. If there is nothing to delete then it will be return false.

getBase64File()

Get base 64 string from file name.

public getBase64File(string $filename) : string

Warning: it can be trigger memory limit exceeded error if file size is too large.

Parameters
$filename : string

The file name to get contents. This will be connect with root in constructor.

Tags
link

Original source code.

since
1.1.4
Return values
string

Return base 64 file content or empty string if not found.

getFileExtensionOnly()

Get file extension only.

public getFileExtensionOnly(string $path) : string
Parameters
$path : string

File name (full path or related path) to get only file extension.

Tags
since
1.1.3
Return values
string

Return only file extension without dot.

getFileNameExtension()

Get file name with extension. No path included.

public getFileNameExtension(string $path) : string

Not check for file exists.

Parameters
$path : string

Full path to file.

Tags
since
1.1.4
Return values
string

Return only file.ext from /path/to/file.ext

getFileNameOnly()

Get file name only.

public getFileNameOnly(string $path) : string
Parameters
$path : string

File name (full path or related path) to get only file name.

Tags
since
1.1.3
Return values
string

Return only file name without extension.

getFolderSize()

Get total folder size.

public getFolderSize(string $path) : int
Parameters
$path : string

Path to the file that is not included with $root in the constructor. No need to prepend with slash.

Tags
link

Original source code.

Return values
int

Return total bytes.

getFullPathWithRoot()

Connect the specify path with root on constructor and get its full path.

public getFullPathWithRoot(string $path) : string

Example: root path on constructor is /var/www/myproject and path argument on this method is images/vangogh.jpg
The result will be /var/www/myproject/images/vangogh.jpg

Parameters
$path : string

The path to get full path with root.

Tags
since
1.1.4
Return values
string

Return full path that was connected with root.

getTimestamp()

Get file or folder timestamp.

public getTimestamp(string $path) : int|false
Parameters
$path : string

Path to the file that is not included with $root in the constructor. No need to prepend with slash.

Return values
int|false

Return timestamp or false on failure.

isDir()

Check for is file exists and it is folder (or directory).

public isDir(string $path[, bool|null $caseSensitive = null ]) : bool
Parameters
$path : string

Path to the folder (or directory) to check, it must be inside the $root in class constructor.

$caseSensitive : bool|null = null

Set to true to check by case sensitive, false to case insensitive, null means up to the OS. The null value will be use PHP is_dir() function.

Return values
bool

Returns true if the filename exists and is a directory, false otherwise.

isFile()

Check for is file exists and it is file.

public isFile(string $path[, bool|null $caseSensitive = null ]) : bool
Parameters
$path : string

Path to the file to check, it must be inside the $root in class constructor.

$caseSensitive : bool|null = null

Set to true to check by case sensitive, false to case insensitive, null means up to the OS. The null value will be use PHP is_file() function.

Return values
bool

Returns true if the filename exists and is a file, false otherwise.

listFiles()

List all files only, folders only, files and folders in single level of specified $dirname.

public listFiles(string $dirname[, string $filterType = '' ]) : array<string|int, mixed>
Parameters
$dirname : string

Path to folder inside the root.

$filterType : string = ''

Filter type of listing. Accept: 'files', 'folders', '' (empty string or all files and folders). Default is empty string.

Tags
since
0.2.2
Return values
array<string|int, mixed>

Return the array list of files (or folders). Example: if dirname is "a" then result will be array(0 => a/file1.txt, 1 => a/folder1, 2 => a/file2.txt). The return value will be relative path from $root.

listFilesSubFolders()

List all files and subfolders in specified $dirname.

public listFilesSubFolders(string $dirname[, array<string|int, mixed> &$files = [] ]) : array<string|int, mixed>
Parameters
$dirname : string

Path to folder inside the root.

$files : array<string|int, mixed> = []

For access currently listed folders & files. Do not set value to this argument.

Return values
array<string|int, mixed>

Return the array list of files and sub folders. Example: if dirname is "a" then result will be array(0 => a/1, 1 => a/1/1.1, 2 => a/1/1.1/1.1.txt, 3 => a/aa, 4 => a/aa/aaa). The return value will be relative path from $root.

rename()

Rename a file or directory.

public rename(string $oldName, string $newName[, array<string|int, mixed> $options = [] ]) : bool
Parameters
$oldName : string

The old name. Related from root specified in class constructor.

$newName : string

The new name. Related from root specified in class constructor.

$options : array<string|int, mixed> = []

Available options:
checkOldNameExists (bool) Set to false for skip check file exists for old name before rename and it will be use PHP's rename() function behavior. Set to true to check before. Default is true.
checkNewNameExists (bool) Set to false for skip check file exists for new name before rename and it will be use PHP's rename() function behavior. Set to true to check before. Default is true.

Tags
since
1.1.2
see
https://www.php.net/manual/en/function.rename.php

PHP rename function behavior about overwritten and emit a warning.

Return values
bool

Return true on success, false on failure.

setWebSafeFileName()

Set web safe file name.

public setWebSafeFileName(string $file) : string

Allowed characters: 0-9, a-z, -, _, . (alpha-numeric, dash, underscore, dot).
Replace multiple spaces to one space
Replace space to dash
Replace not allowed characters to empty
Replace multiple dashes to one dash
Replace multiple dots to one dot.

Parameters
$file : string

The entered file name to rename.

Tags
since
1.1.2
Return values
string

Return formatted for web safe file name.

writeFile()

Write file. (Create a file).

public writeFile(string $path, string $contents) : mixed
Parameters
$path : string

Path to the file to create and write that is not included with $root in the constructor.

$contents : string

The file contents.

Return values
mixed

Returns the number of bytes that were written to the file, or false on failure.

removeUpperPath()

Remove any upper path string such as ../ from string.

protected removeUpperPath(string $path) : string
Parameters
$path : string
Return values
string

        
On this page

Search results