Download the PHP package spoova/filemanager without Composer
On this page you can find all versions of the php package spoova/filemanager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package filemanager
FileManager
The Filemanager class has some useful features which allows the reading and writing into editable files, transferring of files and zipping of files or directories.
Initializing class
This class can be initialized as shown below
Create new directory
A new directory can only be created by supplying the full path of the new directory with the method. This method does not also update the last directory defined within the class. It only adds a new directory if it does not exist and returns true if the directory has been created or it already exists.
Setting url for activity
Aside from few methods like and methods which have stand-alone capabilities of overiding default urls, when using Filemanger for activities, a url or file path is expected to be defined which helps the Filemanager class to know where the operation is expected to be performed. A directory or file path can be specified with the method. An example is shown below:
Create a new file
To create a new file, we can use the method. This method will only create a file if the file does not exist. A boolean of true will be returned if the file created is readable.
- a boolean value of true will allow creating a directory if the expected file destination does not already exist while false will prevent creating a new directory for a file if the directory does not already exist
-
an optional stand-alone file path for file to be created if not earlier defined with .
We can also specify the file path from the setUrl() method as shown below
Create multiple files
To create multiple files, we can use the method. The first argument is an array list of full file paths of files expected to be created. The method will create directories for files if the directories do not exist and it only fails when an error is encountered and a file cannot be created. It returns true if all files were created successfully.
- array list of file paths to be created
- an optional variable that contains lists of successfully created file paths.
Working with files and directories
In order to work with a directory, the directory must be defined where an activity is expected to be performed using the method. Once the source url is defined, we can proceed with the activity.
Set a url
Get folders in specified directory
Folder names only can be harvested without their full paths
Get files in specifed directory
File names and extension can be harvested without their full paths as shown below
We can also get names of files without their extensions. However, it is important to always get extension names to know the type of file
Get contents of a directory
Zip a directory
To zip a url, the methods can be applied with the syntax below
- refers to the output name of the zip file. It uses default specified url if not defined.
-
refers to array list of file name in specified directory that should not be added to zipped file.
We can check if a file is zipped by using the method. However, this will not tell if the zipped file is broken.
We can get the last directory specified using the method.
Copy from a directory to another
To copy from last specified directory to another directory we can apply the method. This will copy the entire folder to another specified directory
- refers to the file or folder destination.
- refers to new name of file. This is optional
-
boolean of true prevents any previous error from stopping operation.
Move last specified directory or file to another location
To move the last specified directory to another location, the can be applied. This is mostly useful when we want to move a zipped file to another location.
- refers to the new file or folder destination.
- refers to file or folder names within the last specified url to be ignored from being moved.
-
this is used to store the names of moved files.
An example of the above is shown below
Move last declared directory to another directory with a new name
To move a last declared path to another directory while specifying a new output name, the can be applied.
- refers to new destination directory where last detected path will be moved into.
- Optional. Refers to new output name of a file if specified. This is useful when $newdir is a file path and not directory. If not specified, the moved file will maintain the original file name.
-
boolean of true prevents any previous error from stopping operation.
An example of the above is shown below
Advanced moving of files and directories
A more advanced method for moving files or directories to another is the method. The basic syntax is show below:
- refers to new destination directory of selected file (or directory) or refers to the subpath of selected directory to be moved into destination directory $param2 if $param2 is defined.
-
refers to destination path of $param1 only if it is supplied.
The example shown below is that in which a directory or file was moved to another directory.
We can also specify that a directory within a primarily selected directory should be moved to another relative or absolute directory as shown below:
The method above will return true or false based on whether the file transfer was successful.
Deleting a file or directory
To delete a folder or file, the method can be employed
-
refers to the path of file or directory to be deleted. If not specified, it assumes the last specified directory.
An example of the above is shown below
Deleting a file only
To delete a file only, the method can be employed
- refers to the path of file or directory to be deleted.
-
If set as true, will return true if specified file does not exist instead of false.
An example of the above is shown below
Extract from a zip file
To extract a non-protected zipped file to a directory, the method can be applied.
- refers to source path if is specified or destination path if is not specified.
-
boolean of true prevents any previous error from stopping operation.
An example of the above is shown below
Working with text readable files
Reading from files is usually done by specifying a key and a separator character. By default, Filemanager uses the colon character to differentiate between keys and values. In some cases the default character is set as as in the case of method. We can read from a readable file by using the and method.
Reading from text file
- name of key
-
specified separator character.
Assuming we have a file user.txt like below :
we can get the contents of the text file using the format below
By default, the delimiter is usually removed whether specified or not. We can also read all contents with as shown below :
If a key that does not exist is being searched, the returned value will be an empty string
Editing a text file
There are five different ways of modifying text files which include the use of , , , and methods.
textLine()
This method is used to write a new line into text file with options before or after a specified line.
- number of new lines to be added to text file.
-
specifies where the lines should be added.
Assuming we have a file user.txt, we can add new lines to the file using the format below
If the file already contains a text, we can specify the position of line using or as shown below :
textWrite()
This method is used to write texts into a text file
- array list of keys and values pairs
-
used to define position and separator character
Assuming we have a file user.txt, we can add new keys to the file using the format below :
We can specify separator character through supplied options with key. Also if the file already contain keys, we can specify the position of new keys with or options as shown below :
textUpdate()
This method is used to update the values of already existing keys in a text file. If the specified keys do not exist, then, the keys along with their values will be added on new lines
- array list of keys and values pairs
- used to store only updated values
-
used to set separator character. Default is sign.
Assuming we have a file user.txt, we can add or update keys of the file using the format below :
The resulting text file will look like the format below
textReplace()
This method is used to replace a key's value only if that key exists.
- array list of keys and values pairs
-
used to set separator character. Default is
Assuming we have a file user.txt, we can replace the existing keys' value :
The resulting text file will look like the format below
Reading a file without instantiating the class
We can read a readable file without instantiating the class itself by using the method. This method will read all the contents of a file which is equivalent to the method.
- path of the file to be read
-
a key to value separator character
Reading a file into the global ENV environment
Readable file keys and value can be stored into the global environment using the method. By default, values are stored under the key. This method is mainly used to read from files.
- path of the file to be read
- default key is ':ENV' . While boolean value of true will store data directly to , false prevents overwriting predefined default $_ENV key. If a custom string name is defined, the name will be used as replacement for the default ':ENV'.
-
a key to value separator character. Note that the default character for this method is set as equal sign rather than semicolon.
Detecting ENV key
The method is used to detect the last custom key used in method. By default this returns .
Detecting ENV data
The method is used to detect the last data saved into the global variable when method was used.
Fetching errors
The method returns the last error detected if an error occurs within the Filemanager system. However, when working with zip files, the and succeeds()
method can help to detect the error that occurs in the process of zipping or decompressing a file. Example of usage is shown below
$Filemanager->setUrl('some/dir');
$Filemanager->zipUrl();
if($Filemanager->succeeds()) {
echo 'Zipping successful';
}elseif($Filemanager->fails()) {
echo $Filemanager->error();
} else {
echo 'Something is wrong!';
}