FilesManager

Extends \org\turbocommons\src\main\php\model\BaseStrictClass

Class that contains the most common file system interaction functionalities

package

Default

Methods

Protection to prevent accessing undefined properties to this class

__get(string $name) : void
inherited

Arguments

$name

string

The property name

Protection to prevent creating extra properties to this class

__set(string $name, string $value) : void
inherited

Arguments

$name

string

The property name

$value

string

The property value

Copy all the contents from a source directory to a destination one (Both source and destination paths must exist).

copyDirectory(string $sourcePath, string $destPath, boolean $destMustBeEmpty = true) : boolean

Any source files that exist on destination will be overwritten without warning. Files that exist on destination but not on source won't be modified, removed or altered in any way.

Throws
\UnexpectedValueException

Arguments

$sourcePath

string

The full path to the source directory where files and folders to copy exist

$destPath

string

The full path to the destination directory where files and folders will be copied

$destMustBeEmpty

boolean

if set to true, an exception will be thrown if the destination directory is not empty.

Response

boolean

True if copy was successful, false otherwise

Copies a file from a source location to the defined destination If the destination file already exists, it will be overwritten.

copyFile(string $sourcePath, string $destPath) : boolean

Arguments

$sourcePath

string

The full path to the source file that must be copied (including the filename itself).

$destPath

string

The full path to the destination where the file must be copied (including the filename itself).

Response

boolean

Returns true on success or false on failure.

Create a directory at the specified filesystem path

createDirectory(string $path, boolean $recursive = false) : boolean

Arguments

$path

string

The full path to the directoy we want to create. For example: c:\apps\my_new_folder

$recursive

boolean

Allows the creation of nested directories specified in the pathname. Defaults to false.

Response

boolean

Returns true on success or false if the folder already exists (an exception may be thrown if a file exists with the same name or folder cannot be created).

Create a TEMPORARY directory on the operating system tmp files location, and get us the full path to access it.

createTempDirectory(string $desiredName, boolean $deleteOnExecutionEnd = true) : string

OS should take care of its removal but it is not assured, so it is recommended to make sure all the tmp data is deleted after using it (This is specially important if the tmp folder contains sensitive data).

Arguments

$desiredName

string

A name we want for the new directory to be created. If name is not available, a unique one (based on the provided desired name) will be generated automatically.

$deleteOnExecutionEnd

boolean

Defines if the generated temp folder must be deleted after the current application execution finishes. Note that when files inside the folder are still used by the app or OS, exceptions or problems may happen, and it is not 100% guaranteed that the folder will be always deleted. So it is better to always handle the temporary folder removal in our code

Response

string

The full path to the newly created temporary directory, including the directory itself (without a trailing slash). For example: C:\Users\Me\AppData\Local\Temp\MyDesiredName

TODO

createTempFile() 

Delete a directory from the filesystem and return a boolean telling if the directory delete succeeded or not Note: All directory contents, folders and files will be also removed.

deleteDirectory(string $path, string $deleteDirectoryItself = true) : boolean

Arguments

$path

string

The path to the directory

$deleteDirectoryItself

string

Set it to true if the specified directory must also be deleted.

Response

boolean

Returns true on success or false on failure.

Delete a filesystem file.

deleteFile(string $path) : boolean

Arguments

$path

string

The file filesystem path

Response

boolean

Returns true on success or false on failure.

Delete a list of filesystem files.

deleteFiles(array $paths) : boolean

Arguments

$paths

array

A list of filesystem paths to delete

Response

boolean

Returns true on success or false if any of the files failed to be deleted

Gives us the current OS directory separator character, so we can build cross platform file paths

dirSep() : string

Response

string

The current OS directory separator character

Find all the elements on a directory which name matches the specified regexp pattern

findDirectoryItems(string $path, string $searchRegexp, string $returnFormat = 'relative', string $searchItemsType = 'both', integer $depth = -1) : array

Arguments

$path

string

A directory where the search will be performed

$searchRegexp

string

A regular expression that files or folders must match to be included into the results. Here are some useful patterns:
'/..txt$/i' - Match all items which name ends with '.txt' (case insensitive)
'/^some.
./' - Match all items which name starts with 'some'
'/text/' - Match all items which name contains 'text'
'/^file.txt$/' - Match all items which name is exactly 'file.txt' '/^..(jpg|jpeg|png|gif)$/i' - Match all items which name ends with .jpg,.jpeg,.png or .gif (case insensitive) '/^(?!..(jpg|png|gif)$)/i' - Match all items that do NOT end with .jpg, .png or .gif (case insensitive)

$returnFormat

string

Defines how will be returned the array of results. Three values are possible:

  • If set to 'name' each result element will contain its file (with extension) or folder name
  • If set to 'relative' each result element will contain its file (with extension) or folder name plus its path relative to the search root
  • If set to 'absolute' each result element will contain its file (with extension) or folder name plus its full OS absolute path

$searchItemsType

string

Defines the type for the directory elements to search: 'files' to search only files, 'folders' to search only folders, 'both' to search on all the directory contents

$depth

integer

Defines the maximum number of subfolders where the search will be performed:

  • If set to -1 the search will be performed on the whole folder contents
  • If set to 0 the search will be performed only on the path root elements
  • If set to 2 the search will be performed on the root, first and second depth level of subfolders

Response

array

A list formatted as defined in returnFormat, with all the elements that meet the search criteria

Search for a folder name that does not exist on the provided path.

findUniqueDirectoryName(string $path, string $desiredName = '', string $text = '', string $separator = '-', string $isPrefix = false) : string

If we want to create a new folder inside another one without knowing for sure what does it contain, this method will guarantee us that we have a unique directory name that does not collide with any other folder or file that currently exists on the path.

NOTE: This method does not create any folder or alter the given path in any way.

Arguments

$path

string

The full path to the directoy we want to check for a unique folder name

$desiredName

string

We can specify a suggested name for the unique directory. This method will verify that it does not exist, or otherwise give us a name based on our desired one that is unique for the path

$text

string

Text that will be appended to the suggested name in case it already exists. For example: text='copy' will generate a result like 'NewFolder-copy' or 'NewFolder-copy-1' if a folder named 'NewFolder' exists

$separator

string

String that will be used to join the suggested name with the text and the numeric file counter. For example: separator='---' will generate a result like 'NewFolder---copy---1' if a folder named 'NewFolder' already exists

$isPrefix

string

Defines if the extra text that will be appended to the desired name will be placed after or before the name on the result. For example: isPrefix=true will generate a result like 'copy-1-NewFolder' if a folder named 'NewFolder' already exists

Response

string

A directory name that can be safely created on the specified path, cause no one exists with the same name (No path is returned by this method, only a directory name. For example: 'folder-1', 'directoryName-5', etc..).

Search for a file name that does not exist on the provided path.

findUniqueFileName(string $path, string $desiredName = '', string $text = '', string $separator = '-', string $isPrefix = false) : string

If we want to create a new file inside a folder without knowing for sure what does it contain, this method will guarantee us that we have a unique file name that does not collide with any other folder or file that currently exists on the path.

NOTE: This method does not create any file or alter the given path in any way.

Arguments

$path

string

The full path to the directoy we want to check for a unique file name

$desiredName

string

We can specify a suggested name for the unique file. This method will verify that it does not exist, or otherwise give us a name based on our desired one that is unique for the path

$text

string

Text that will be appended to the suggested name in case it already exists. For example: text='copy' will generate a result like 'NewFile-copy' or 'NewFile-copy-1' if a file named 'NewFile' exists

$separator

string

String that will be used to join the suggested name with the text and the numeric file counter. For example: separator='---' will generate a result like 'NewFile---copy---1' if a file named 'NewFile' already exists

$isPrefix

string

Defines if the extra text that will be appended to the desired name will be placed after or before the name on the result. For example: isPrefix=true will generate a result like 'copy-1-NewFile' if a file named 'NewFile' already exists

Response

string

A file name that can be safely created on the specified path, cause no one exists with the same name (No path is returned by this method, only a file name. For example: 'file-1', 'fileName-5', etc..).

Gives the list of items that are stored on the specified folder. It will give files and directories, and each element will be the item name, without the path to it.

getDirectoryList(string $path, string $sort = '') : array

The contents of any subfolder will not be listed. We must call this method for each child folder if we want to get it's list. (The method ignores the . and .. items if exist).

Arguments

$path

string

Full path to the directory we want to list

$sort

string

Specifies the sort for the result:
  '' will not sort the result.
  'nameAsc' will sort the result by filename ascending.   'nameDesc' will sort the result by filename descending.   'mDateAsc' will sort the result by modification date ascending.   'mDateDesc' will sort the result by modification date descending.

Response

array

The list of item names inside the specified path sorted as requested, or an empty array if no items found inside the folder.

Calculate the full size in bytes for a specified folder and all its contents.

getDirectorySize(string $path) : integer

Arguments

$path

string

Full path to the directory we want to calculate its size

Response

integer

the size of the file in bytes. An exception will be thrown if value cannot be obtained

Get the size from a file

getFileSize(string $path) : integer

Arguments

$path

string

The file full path, including the file name and extension

Response

integer

the size of the file in bytes. An exception will be thrown if value cannot be obtained

Check if the specified path is a directory or not.

isDirectory(string $path) : boolean

Arguments

$path

string

An Operating system path to test

Response

boolean

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

Checks if the specified folder is empty

isDirectoryEmpty(string $path) : boolean

Arguments

$path

string

The path to the directory we want to check

Response

boolean

True if directory is empty, false if not. If it does not exist or cannot be read, an exception will be generated

Check if two directories contain exactly the same folder structure and files.

isDirectoryEqualTo(string $path1, string $path2) : boolean

Arguments

$path1

string

The full path to the first directory to compare

$path2

string

The full path to the second directory to compare

Response

boolean

true if both paths are valid directories and contain exactly the same files and folders tree.

Check if the specified path is a file or not.

isFile(string $path) : boolean

Arguments

$path

string

An Operating system path to test

Response

boolean

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

Check if two provided files are identical

isFileEqualTo(string $file1, string $file2) : boolean
Throws
\UnexpectedValueException

Arguments

$file1

string

The first file to compare

$file2

string

The second file to compare

Response

boolean

True if both files are identical, false otherwise

Concatenate all the provided files, one after the other, into a single destination file.

mergeFiles(array $sourcePaths, string $destFile, string $separator = '') : boolean

Arguments

$sourcePaths

array

A list with the full paths to the files we want to join. The result will be generated in the same order.

$destFile

string

The full path where the merged file will be stored, including the full file name (will be overwitten if exists).

$separator

string

An optional string that will be concatenated between each file content. We can for example use "\n\n" to create some empty space between each file content

Response

boolean

True on success or false on failure.

Read and return the content of a file. Not suitable for big files (More than 5 MB) cause the script memory may get full and the execution fail

readFile(string $path) : string

Arguments

$path

string

An Operating system full or relative path containing some file

Response

string

The file contents as a string. If the file is not found or cannot be read, an exception will be thrown.

Reads a file and performs a buffered output to the browser, by sending it as small fragments.<br> This method is mandatory with big files, as reading the whole file to memory will cause the script or RAM to fail.<br><br>

readFileBuffered(string $path, float $downloadRateLimit) : integer

Adapted from code suggested at: http://php.net/manual/es/function.readfile.php

Arguments

$path

string

The file full path

$downloadRateLimit

float

If we want to limit the download rate of the file, we can do it by setting this value to > 0. For example: 20.5 will set the file download rate to 20,5 kb/s

Response

integer

the number of bytes read from the file.

Renames a directory.

renameDirectory(string $sourcePath, string $destPath) : boolean

Arguments

$sourcePath

string

The full path to the source directory that must be renamed (including the directoy itself).

$destPath

string

The full path to the new directoy name (including the directoy itself). It must not exist.

Response

boolean

true on success or false on failure.

Renames a file.

renameFile(string $sourcePath, string $destPath) : boolean

Arguments

$sourcePath

string

The full path to the source file that must be renamed (including the filename itself).

$destPath

string

The full path to the new file name (including the filename itself). It must not exist.

Response

boolean

True on success or false on failure.

Writes the specified data to a physical file, which will be created (if it does not exist) or overwritten without warning.

saveFile(string $pathToFile, string $data = '', string $append = false) : True

This method can be used to create a new empty file, a new file with any contents or to overwrite an existing one.

We must check for file existence before executing this method if we don't want to inadvertently replace existing files.

see \org\turbocommons\src\main\php\managers\FilesManager::isFile

Arguments

$pathToFile

string

The path including full filename where data will be saved. File will be created or overwritten without warning.

$data

string

Any information to save on the file.

$append

string

Set it to true to append the data to the end of the file instead of overwritting it. File will be created if it does not exist, even with append set to true.

Response

True

on success or false on failure.

TODO implement this method

syncDirectories(\org\turbocommons\src\main\php\managers\string $path1, \org\turbocommons\src\main\php\managers\string $path2) 

Arguments

$path1

\org\turbocommons\src\main\php\managers\string

$path2

\org\turbocommons\src\main\php\managers\string