Class that contains the most common file system interaction functionalities
package |
Default |
---|
__get(string $name) : void
string
The property name
__set(string $name, string $value) : void
string
The property name
string
The property value
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 |
|
---|
string
The full path to the source directory where files and folders to copy exist
string
The full path to the destination directory where files and folders will be copied
boolean
if set to true, an exception will be thrown if the destination directory is not empty.
boolean
True if copy was successful, false otherwise
copyFile(string $sourcePath, string $destPath) : boolean
string
The full path to the source file that must be copied (including the filename itself).
string
The full path to the destination where the file must be copied (including the filename itself).
boolean
Returns true on success or false on failure.
createDirectory(string $path, boolean $recursive = false) : boolean
string
The full path to the directoy we want to create. For example: c:\apps\my_new_folder
boolean
Allows the creation of nested directories specified in the pathname. Defaults to false.
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).
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).
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.
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
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
createTempFile()
deleteDirectory(string $path, string $deleteDirectoryItself = true) : boolean
string
The path to the directory
string
Set it to true if the specified directory must also be deleted.
boolean
Returns true on success or false on failure.
deleteFile(string $path) : boolean
string
The file filesystem path
boolean
Returns true on success or false on failure.
deleteFiles(array $paths) : boolean
array
A list of filesystem paths to delete
boolean
Returns true on success or false if any of the files failed to be deleted
dirSep() : string
string
The current OS directory separator character
findDirectoryItems(string $path, string $searchRegexp, string $returnFormat = 'relative', string $searchItemsType = 'both', integer $depth = -1) : array
string
A directory where the search will be performed
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)
string
Defines how will be returned the array of results. Three values are possible:
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
integer
Defines the maximum number of subfolders where the search will be performed:
array
A list formatted as defined in returnFormat, with all the elements that meet the search criteria
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.
string
The full path to the directoy we want to check for a unique folder name
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
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
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
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
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..).
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.
string
The full path to the directoy we want to check for a unique file name
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
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
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
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
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..).
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).
string
Full path to the directory we want to list
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.
array
The list of item names inside the specified path sorted as requested, or an empty array if no items found inside the folder.
getDirectorySize(string $path) : integer
string
Full path to the directory we want to calculate its size
integer
the size of the file in bytes. An exception will be thrown if value cannot be obtained
getFileSize(string $path) : integer
string
The file full path, including the file name and extension
integer
the size of the file in bytes. An exception will be thrown if value cannot be obtained
isDirectory(string $path) : boolean
string
An Operating system path to test
boolean
true if the path exists and is a directory, false otherwise.
isDirectoryEmpty(string $path) : boolean
string
The path to the directory we want to check
boolean
True if directory is empty, false if not. If it does not exist or cannot be read, an exception will be generated
isDirectoryEqualTo(string $path1, string $path2) : boolean
string
The full path to the first directory to compare
string
The full path to the second directory to compare
boolean
true if both paths are valid directories and contain exactly the same files and folders tree.
isFile(string $path) : boolean
string
An Operating system path to test
boolean
true if the path exists and is a file, false otherwise.
isFileEqualTo(string $file1, string $file2) : boolean
Throws |
|
---|
string
The first file to compare
string
The second file to compare
boolean
True if both files are identical, false otherwise
mergeFiles(array $sourcePaths, string $destFile, string $separator = '') : boolean
array
A list with the full paths to the files we want to join. The result will be generated in the same order.
string
The full path where the merged file will be stored, including the full file name (will be overwitten if exists).
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
boolean
True on success or false on failure.
readFile(string $path) : string
string
An Operating system full or relative path containing some file
string
The file contents as a string. If the file is not found or cannot be read, an exception will be thrown.
readFileBuffered(string $path, float $downloadRateLimit) : integer
Adapted from code suggested at: http://php.net/manual/es/function.readfile.php
string
The file full path
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
integer
the number of bytes read from the file.
renameDirectory(string $sourcePath, string $destPath) : boolean
string
The full path to the source directory that must be renamed (including the directoy itself).
string
The full path to the new directoy name (including the directoy itself). It must not exist.
boolean
true on success or false on failure.
renameFile(string $sourcePath, string $destPath) : boolean
string
The full path to the source file that must be renamed (including the filename itself).
string
The full path to the new file name (including the filename itself). It must not exist.
boolean
True on success or false on failure.
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 |
---|
string
The path including full filename where data will be saved. File will be created or overwritten without warning.
string
Any information to save on the file.
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.
True
on success or false on failure.
syncDirectories(\org\turbocommons\src\main\php\managers\string $path1, \org\turbocommons\src\main\php\managers\string $path2)
\org\turbocommons\src\main\php\managers\string
\org\turbocommons\src\main\php\managers\string