HelPHP

Filesystem
in package

Tags
class

Filesystem

Our Filesystem class is there to offer two things :

  • first, an universal solution working with any kind of mounted storage (local or distant) that choose the best between native PHP features and linux native OS commande to perform a secured operation without headaches.

  • Secondary : the possibiliy to follow the filesystem process on huge ones (long multiple copy etc), with a sql or nosql server depending your configuration.

Of course it can be a real good backend base for a cloud service with user/group rights verification etc, and you'll find in the instance API, all you need to pilot this class with Rest queries and in the instance test/rest folder, an api client with all Fs commands ready to try. This client is calling Restresponse Class, this class is full of exemple related to the FS.

Init.php call its create instance method, so the global $FS is available everywhere in any HelPHP componant/modules.

one important thing is the root_fs : in the config of the instance, is set its value, indicating where all operations should happened. the FS will refuse to do any operation outside this root_fs, so it's the root of your users virtual storage. then there is a user / group rights to consider to authorise each operation.

Manage also images/files sequences with file names formated like this : basename[xxxx-yyyy].ext

Table of Contents

Properties

$bad_name  : mixed
$err_lst  : mixed
$file_is_locked  : mixed
$follow_redis  : mixed
$last_checked_type  : mixed
$last_error  : mixed
$last_errors  : mixed
$lock_time  : mixed
$root_fs  : mixed
$save_history  : mixed
$skip_check_path  : mixed
$skip_lock  : mixed
$unauthorized_character  : mixed
$unauthorized_character_string  : mixed
$with_permission  : mixed

Methods

__construct()  : mixed
add_lock()  : void
add_lock add a "lock" on files/folders The lock indicates that a file/folder is being used (moving, copying, depressing...) and that I cannot touch it It is to prevent dual use, or deletion by one user while another had started a copy or other.
bin_add()  : mixed
to add in the bin if you have already created $CONFIG::ROOT_FS.'¤bin¤' folder
bin_clear()  : bool
Clean the trash bin
bin_remove()  : mixed
delete definitly from the bin
bin_restore()  : bool
to restore a list of file out of the bin
check_lock()  : bool
Will simply return the lock state of a path
check_name()  : bool
Check if the string can be used as a filename
check_path()  : bool
verify if it's an accessible and authorized path
check_permission()  : string|array<string|int, mixed>
check_permission to read write delete in module folder the module can be here, but a user can have no right to access it
clean_filter()  : mixed
copy()  : bool
copy the "sources" to the destination folder.
create_instance()  : object
Create an instance in the global $FS
delete()  : bool
One command for files or folders and with autorization check.
delete_lock()  : void
Delete the lock on a path.
delete_with_follow()  : mixed
Delete anything with process follow after autorization check
du()  : mixed
Return available space storage available quickly ! Some storages like CEPHFS, depending their configuration do not return Bytes.
exist()  : bool
A little function to replace is_file is_folder is_link
get_file_ext()  : string
Return the file extention
get_file_name()  : string
return the filename
get_file_name_noext()  : string
return the filename without extention
get_file_path()  : string
remove the filename and return the path
get_files_from_sequence()  : array<string|int, mixed>
Search all files corresponding to sequence description
get_info()  : array<string|int, mixed>
return available infos on folder link or file
get_modified()  : array<string|int, mixed>
Get modified items since a given date
get_sequence_base_name()  : string
Return the basename without extention
get_sequence_data()  : array<string|int, mixed>
Get all possible infos about a sequence
home_du()  : array<string|int, mixed>
Return available storage assiociated with the home folder of the instance.
human_readable_size()  : string
Convert number of bytes in readable format
init_db()  : mixed
Check and create table needed for the filesystem to work when redis isn't active.
init_redis()  : mixed
Check and connect to redis
mkdir()  : bool
A classic Mkdir plus path authorization checking
move()  : array<string|int, mixed>
Move work a little bit like copy and should be used also for rename
pack()  : array<string|int, mixed>
Will create an archive
pre_or_suf_fixing()  : string
to add a prefix or suffix on filename in a full path (prefix by default) (just string manipulation, the file is not mv/rename with this function)
recurse_ls()  : array<string|int, mixed>
Return an array tree with available files / folders / links in each folder.
replace_file_name()  : string
Return a new path with the name of the file replaced securely, without touching to extension : avoid str_replace error, when the file name can be found also in the path
save_content()  : mixed
save_content do the same job as file_put_contents, but make a check to verify if it can write, and if it's not the case, can optionnaly stop the execution .( file_put_contents do no stop execution)
shell_ls()  : array<string|int, mixed>
ls from shell with grep filter to make quick search and return all infos possible on files/folders/links and sequences if you just need names without details and sequences, check recurse_ls() for file or folder with ¤marker¤, to detect them use this on the output: `preg_match('/^¤{1}.+?¤{1}/', $file_name)`
unpack()  : bool
To unpack an archive
update_history()  : mixed
If save_history is true, we send process time consuming recognize by its process key to history.
update_lock()  : void
Updating the lock will renew its expiration date... so during long operation it's important to update the locks at regular interval (depending the config)

Properties

$file_is_locked

public mixed $file_is_locked = false

$follow_redis

public mixed $follow_redis = false

$last_checked_type

public mixed $last_checked_type = ''

$save_history

public mixed $save_history = false

$skip_check_path

public mixed $skip_check_path = true

$unauthorized_character

public mixed $unauthorized_character = ['/', '\\', '<', '>', '*', '?', '"', ':', '|', '¤']

$unauthorized_character_string

public mixed $unauthorized_character_string = '/\<>*?":|¤'

$with_permission

public mixed $with_permission = false

Methods

__construct()

public __construct([mixed $root_fs = false ]) : mixed
Parameters
$root_fs : mixed = false

add_lock()

add_lock add a "lock" on files/folders The lock indicates that a file/folder is being used (moving, copying, depressing...) and that I cannot touch it It is to prevent dual use, or deletion by one user while another had started a copy or other.

public add_lock(string $path, int $timestamp[, bool $parent = true ]) : void

The lock also applies to the relatives of the target. If I copy /fold1/fold2/ and during this time /fold1/ is deleted by someone, there will be some tears !!! $parent determines if we lock the parent too, true by default

the lock time is fixed in the main config of the instance

Parameters
$path : string
$timestamp : int

current time in general to start the lock

$parent : bool = true

=true

bin_add()

to add in the bin if you have already created $CONFIG::ROOT_FS.'¤bin¤' folder

public bin_add(mixed $paths[, string $key = '' ][, mixed $api = false ]) : mixed
Parameters
$paths : mixed
$key : string = ''
$api : mixed = false

=false

Tags
@return

[type]

bin_clear()

Clean the trash bin

public bin_clear([string $key = '' ]) : bool
Parameters
$key : string = ''
Return values
bool

true

bin_remove()

delete definitly from the bin

public bin_remove(array<string|int, mixed> $lst, mixed $key[, bool $api = false ]) : mixed
Parameters
$lst : array<string|int, mixed>

of paths

$key : mixed
$api : bool = false
Tags
@return

[type]

bin_restore()

to restore a list of file out of the bin

public bin_restore(array<string|int, mixed> $lst[, string $key = '' ]) : bool
Parameters
$lst : array<string|int, mixed>

of paths

$key : string = ''
Return values
bool

ok

check_lock()

Will simply return the lock state of a path

public check_lock(string $path) : bool
Parameters
$path : string
Return values
bool

check_name()

Check if the string can be used as a filename

public check_name(mixed $name) : bool
Parameters
$name : mixed
Return values
bool

check_path()

verify if it's an accessible and authorized path

public check_path(string $path[, bool $skip_lock = false ][, string $type = '' ][, mixed $skip_exist = false ]) : bool

Each detected error is pushed to err_lst array. err_list should be emptied before each call to check_path if you don't want to get previous errors.

Parameters
$path : string
$skip_lock : bool = false

=false true | false, when displaying we don't need to check the redis lock

$type : string = ''

type of action for check_permission write | read | delete | move | new_folder If type given, will return the path otherwise will return an array with each permission and the path

$skip_exist : mixed = false

=false

Return values
bool

true if ok .

check_permission()

check_permission to read write delete in module folder the module can be here, but a user can have no right to access it

public check_permission(string $path, string $type) : string|array<string|int, mixed>
Parameters
$path : string
$type : string

if '' return a detailed array of user rights

Return values
string|array<string|int, mixed>

path o detail of user rights

clean_filter()

public clean_filter(mixed $filter) : mixed
Parameters
$filter : mixed

copy()

copy the "sources" to the destination folder.

public copy(array<string|int, mixed> $sources, string $destination[, mixed $no_progress = false ][, mixed $key = false ]) : bool

can be simply used like this : $FS->copy($file,$destination); but can take care of a bunch of files

Parameters
$sources : array<string|int, mixed>

for a single file, it can be a simple path string. but it can be also an array with for each file : path => the file path , name => a new name, replace => a bool to indicate if we overwrite or not a possibly existing file

$destination : string

@param bool $no_progress=false to indicate not to go through the system_to_process_to_redis and to stay in the same php execution pipe. @param bool|string $key to follow the process identified by this $key

in the case of a copy with a file that must change its name on arrival because it conflicts with an already present file We use two variables $cmd_before and $cmd_after, to save the mv commands needed to rename the copied file

  1. $cmd_before -> Rename the file in the destination to ¤tokeep¤FILE_NAME (be careful not to forget the linked folders like ¤proxy¤FOLDER_NAME)
  2. $cmd -> rsync the file with the same name from the origin to the destination
  3. $cmd_after -> Renames the copied file to its new name
  4. $cmd_after -> Rename the ¤tokeep¤FILE_NAME file to FILE_NAME

at the end , all the cmds are sent as one process to follow

$no_progress : mixed = false
$key : mixed = false
Return values
bool

true/false to indicate if all cmds have benn sent to process to follow, it's Utils::follow_system_process_redis($key) (or not with _redis if your prefer an sql follow) that will return correct infos.

create_instance()

Create an instance in the global $FS

public static create_instance([bool $root_fs = false ][, bool $forceNewInstance = false ]) : object
Parameters
$root_fs : bool = false

the root storage folder, no

$forceNewInstance : bool = false
Return values
object

Filesystem instance

delete()

One command for files or folders and with autorization check.

public delete(string $target) : bool

Important : this one do not follow the process advance. It's made for fast action on one item so can't be used on a file/image sequence check delete_with_follow() if needed.

Parameters
$target : string
Return values
bool

true or false

delete_lock()

Delete the lock on a path.

public delete_lock(string $path, int $timestamp[, bool $force = false ][, bool $parent = true ]) : void

It use the path and the timestamp given at lock creation to recognize the lock to delete. If $force is true, it will remove all locks for a given path without using timestamp

Parameters
$path : string
$timestamp : int
$force : bool = false

=false

$parent : bool = true

=true

delete_with_follow()

Delete anything with process follow after autorization check

public delete_with_follow(string|array<string|int, mixed> $paths[, string $key = false ][, bool $api = false ]) : mixed
Parameters
$paths : string|array<string|int, mixed>

one or Array of paths

$key : string = false

=false the key to follow the process

$api : bool = false

=false to follow in history without $key

Tags
@return

[type]

du()

Return available space storage available quickly ! Some storages like CEPHFS, depending their configuration do not return Bytes.

public du(mixed $dir) : mixed

so there is some commented code to test in case of the result seems hilarious.

Parameters
$dir : mixed
Tags
@return

[type]

exist()

A little function to replace is_file is_folder is_link

public exist(string $path) : bool
Parameters
$path : string
Return values
bool

get_file_ext()

Return the file extention

public static get_file_ext(mixed $file) : string
Parameters
$file : mixed
Return values
string

get_file_name()

return the filename

public static get_file_name(mixed $file) : string
Parameters
$file : mixed
Return values
string

get_file_name_noext()

return the filename without extention

public static get_file_name_noext(mixed $file) : string
Parameters
$file : mixed
Return values
string

get_file_path()

remove the filename and return the path

public static get_file_path(mixed $file) : string
Parameters
$file : mixed
Return values
string

get_files_from_sequence()

Search all files corresponding to sequence description

public get_files_from_sequence(mixed $path, mixed $basename, mixed $numberDigit, mixed $ext) : array<string|int, mixed>
Parameters
$path : mixed
$basename : mixed
$numberDigit : mixed
$ext : mixed
Return values
array<string|int, mixed>

of files found

get_info()

return available infos on folder link or file

public get_info(string $path) : array<string|int, mixed>
Parameters
$path : string
Return values
array<string|int, mixed>

get_modified()

Get modified items since a given date

public get_modified(mixed $path, mixed $date) : array<string|int, mixed>
Parameters
$path : mixed
$date : mixed
Return values
array<string|int, mixed>

of paths

get_sequence_base_name()

Return the basename without extention

public get_sequence_base_name(mixed $path) : string
Parameters
$path : mixed
Return values
string

get_sequence_data()

Get all possible infos about a sequence

public get_sequence_data(mixed $path[, mixed $type = '' ]) : array<string|int, mixed>
Parameters
$path : mixed
$type : mixed = ''

='' if you need to force another type (very rare)

Return values
array<string|int, mixed>

home_du()

Return available storage assiociated with the home folder of the instance.

public home_du() : array<string|int, mixed>

some filesystem like glusterFS family can note quota for a folder. so we'll check the available quota from the folder, or the config, and if there is none of them, we indicate 5 GB by default/

Return values
array<string|int, mixed>

: 0 => Global disk storage available 1 => Occupied space 2 => in percentage 3 => remaining space

human_readable_size()

Convert number of bytes in readable format

public static human_readable_size(mixed $byte) : string
Parameters
$byte : mixed
Return values
string

init_db()

Check and create table needed for the filesystem to work when redis isn't active.

public init_db() : mixed

Necessary if you want to keep a filesystem_history

init_redis()

Check and connect to redis

public init_redis() : mixed

mkdir()

A classic Mkdir plus path authorization checking

public mkdir(string $destination[, int $rights = 0775 ][, bool $recursive = true ]) : bool
Parameters
$destination : string

path

$rights : int = 0775

level default is 0755

$recursive : bool = true

true by default

Return values
bool

true or false

move()

Move work a little bit like copy and should be used also for rename

public move(mixed $source, string $destination[, mixed $key = false ][, bool|string $api = false ]) : array<string|int, mixed>
Parameters
$source : mixed
$destination : string
$key : mixed = false
$api : bool|string = false

=false ton indicate if the call is coming from the api, in that case the history is recorded without $key

Return values
array<string|int, mixed>

containing $succes : a list of correctly moved paths, and/or error message in error ['success', 'error']

pack()

Will create an archive

public pack(array<string|int, mixed> $pathList, string $destination, string $name, string $format[, int $level = 0 ][, string $password = '' ][, string $key = '' ]) : array<string|int, mixed>
Parameters
$pathList : array<string|int, mixed>

list of path to compress

$destination : string

destination folder path

$name : string
$format : string

can be 'zip' 'tar' '7z' 'gz'

$level : int = 0

=0 to 10

$password : string = ''

='' if you want to secure your archive a bit

$key : string = ''

='' for following process

Return values
array<string|int, mixed>

success status and error message

pre_or_suf_fixing()

to add a prefix or suffix on filename in a full path (prefix by default) (just string manipulation, the file is not mv/rename with this function)

public pre_or_suf_fixing(string $file, string $presufix[, string $suffixing = false ]) : string
Parameters
$file : string

the path

$presufix : string

the string to prefix or suffix

$suffixing : string = false

=false false : prefix , true : suffix

Return values
string

the new path

recurse_ls()

Return an array tree with available files / folders / links in each folder.

public recurse_ls(string $path[, string $basepath = '' ]) : array<string|int, mixed>
Parameters
$path : string

the starting folder

$basepath : string = ''

='' a string that prefix all result

Return values
array<string|int, mixed>

tree

replace_file_name()

Return a new path with the name of the file replaced securely, without touching to extension : avoid str_replace error, when the file name can be found also in the path

public static replace_file_name(string $file, string $new_name) : string
Parameters
$file : string

the full path of the file

$new_name : string

the string to replace the old name with.

Return values
string

save_content()

save_content do the same job as file_put_contents, but make a check to verify if it can write, and if it's not the case, can optionnaly stop the execution .( file_put_contents do no stop execution)

public static save_content(string $path, mixed $content[, bool $stop = false ][, mixed $rights = 02664 ]) : mixed
Parameters
$path : string

the path to save the file

$content : mixed

to be saved in the fail

$stop : bool = false

=false execution of the current script and alert the permission denied

$rights : mixed = 02664

shell_ls()

ls from shell with grep filter to make quick search and return all infos possible on files/folders/links and sequences if you just need names without details and sequences, check recurse_ls() for file or folder with ¤marker¤, to detect them use this on the output: `preg_match('/^¤{1}.+?¤{1}/', $file_name)`

public shell_ls([string $basePath = '' ][, string $filter = '' ][, bool $get_folders = true ][, bool $get_files = true ][, bool $get_links = true ][, bool $recurse = false ][, bool $ignore_hidden = false ][, bool $childsequence = false ]) : array<string|int, mixed>
Parameters
$basePath : string = ''
$filter : string = ''

to return only element whose contain it in their names

$get_folders : bool = true
$get_files : bool = true
$get_links : bool = true
$recurse : bool = false

default to false, because it's slow, check recurse_ls

$ignore_hidden : bool = false

for removing hidden file or folder from the final result

$childsequence : bool = false
Return values
array<string|int, mixed>

with for each folder : 'type' => 'folder', 'creation-date' => 'date as string', 'name' => 'folder name as string'

for files : 'type' => 'file', 'mime-type'=>'the mime as string', 'modification-date' => 'date as string', 'name' => 'folder name as string', 'size' => size as int

for links : 'type' => 'link', 'creation-date' => 'date as string', 'name' => 'link name as string with target of the link separated by ' -> ';

for sequences : 'type'=>'sequence', 'mime-type'=>'the mime as string', 'modification-date' => 'date as string', 'size' => size as int 'name' => 'sequence name as string', 'start' => Number of the first file in the sequence as int, 'last' => Number of the last file in the sequence as int, 'total' => Number of files as int, 'ext' => file extension as string, 'child' => array containing names of all files.

unpack()

To unpack an archive

public unpack(mixed $path, mixed $destination[, mixed $password = '' ][, mixed $subfolder = false ][, mixed $erase = false ][, mixed $avoid = false ][, mixed $key = '' ]) : bool
Parameters
$path : mixed
$destination : mixed
$password : mixed = ''

=''

$subfolder : mixed = false

=false true if you want to unpack in a folder with the same name as the archive

$erase : mixed = false

=false in case of conflict it will write over the previous files

$avoid : mixed = false

=false update old file and write only files that are not in destination

$key : mixed = ''

='' for following the process

Return values
bool

true if success

update_history()

If save_history is true, we send process time consuming recognize by its process key to history.

public static update_history(string $key) : mixed
Parameters
$key : string

update_lock()

Updating the lock will renew its expiration date... so during long operation it's important to update the locks at regular interval (depending the config)

public update_lock(string $path[, bool $parent = true ]) : void
Parameters
$path : string
$parent : bool = true

=true


        
On this page

Search results