sFire\System\Directory lets you create and manipulate file system directories. It can give you information about the size, permissions, owners and more in a handy interface.
To create a new sFire\System\Directory instance, you call the constructor. The constructor requires a directory as a String.
$dir = new Directory('foo');
With this instance you can chain methods. For example, you can create, rename and move the directory in one go:
new Directory('foo') -> create() -> rename('bar') -> move('new/location/');
To create a new directory, you can use the create method. First create an instance with the directory you want to create and then call the create method. In this example, the "foo" directory will be created in the current working directory.
$dir = new Directory('foo');
$dir -> create();
You can also give the directory permissions on creation. Default the permissions will be 0777.
$dir = new Directory('foo');
$dir -> create(0775);
Note: Notice that mode is not automatically assumed to be an octal value, so to ensure the expected operation, you need to prefix mode with a zero (0). Strings such as "g+w" will not work properly.
The create method will try to create all the directories given.
$dir = new Directory('foo/bar/baz');
$dir -> create(); //If the directory "foo" does not exists, it will create it first before creating "bar" etc.
To retrieve the files and folders of the directory, you can use the getContent method. The content method accepts four types of parameters. Default, it will return the files and folders as a sFire\System\File Object. All directories will be a new instance of sFire\System\Directory and all files will be a new instance of sFire\System\File.
$dir = new Directory('foo');
$dir -> getContent(); //Returns the content as sFire\System\File Object
$dir -> getContent($dir :: TYPE_DEFAULT); Similar of the above
$dir -> getContent($dir :: TYPE_ARRAY); //Returns the content as an Array
$dir -> getContent($dir :: TYPE_JSON); //Returns the content as a Json string
$dir -> getContent($dir :: TYPE_OBJECT); //Returns the content as a Stdclass object
The given directory is stored in sFire\Entity\Directory which enables you to retrieve and set new data for the directory.
$dir = new Directory('foo');
$dir -> entity();
//Output similar to:
sFire\Entity\Directory Object
(
[name:sFire\Entity\Directory:private] => foo
[exists:sFire\Entity\Directory:private] => 1
[accesstime:sFire\Entity\Directory:private] => 1509042799
[path:sFire\Entity\Directory:private] => /var/www/foo
[basepath:sFire\Entity\Directory:private] => /var/www
[group:sFire\Entity\Directory:private] => 1000
[modificationtime:sFire\Entity\Directory:private] => 1508955700
[readable:sFire\Entity\Directory:private] => 1
[owner:sFire\Entity\Directory:private] => 1000
[writable:sFire\Entity\Directory:private] => 1
)
Note: If you set new data, this will only be represented in the entity and will not change anything to the physical directory.
You can move a directory with it's content to a new location. The new location should be an existing directory and should be writable.
$dir -> move('bar'); //Moves the directory to the "bar" directory
To delete a directory, you can use the delete method. This will remove the directory with all it's content.
$dir -> delete(); //Deletes the directory and all it's content
To copy a directory, you can use the copy method. This will copy the directory with all it's content to a new location. The new location should be an existing directory and should be writable.
$dir -> copy('bar'); //Copy the directory and all it's content to the "bar" directory
To rename a directory, you can use the rename method. This will rename the directory to a new given name. The content within the directory will be ignored.
$dir -> rename('bar'); //Renamed the directory to "bar"
To make sure you can write new files and folders in a directory, you should check if the given directory is writable. You can use the isWritable method to return Boolean true if the directory is writable and false if not.
if(true === $dir -> isWritable()) {
//Directory is writable
}
To make sure you can read files and folders in a given directory, you can use the isReadable. This method will return a Boolean true if the directory is readable and false if not.
if(true === $dir -> isReadable()) {
//Directory is readable
}
To make sure a given directory exists, you can use the exists method to return a Boolean true if exists or false if not.
if(true === $dir -> exists()) {
//Directory exists
}
To change the permissions of a directory, you can call the chmod method. This method will return a boolean true on success or false on failure.
$dir = new Directory('foo');
$dir -> chmod(0600); //Read and write for owner, nothing for everybody else
$dir -> chmod(0644); //Read and write for owner, read for everybody else
$dir -> chmod(0755); //Everything for owner, read and execute for others
$dir -> chmod(0750); //Everything for owner, read and execute for owner's group
Note: Note that mode is not automatically assumed to be an octal value, so to ensure the expected operation, you need to prefix mode with a zero (0). Strings such as "g+w" will not work properly.
To change the owner of a directory, you can call the chown method. This method will return a Boolean true on success or false on failure.
$dir -> chown('user'); //Read and write for owner, nothing for everybody else
Note: Only the superuser may change the owner of a directory.
If you want to know the directory size, you can use the getSize method. This will return a Integer with the directory size in bytes.
$dir -> getSize(); //Will output similar to: 5231
The info method will return an Array with global information about a directory.
$info = $dir -> info();
print_r($info);
//Output similar to:
Array
(
[name] => foo
[exists] => 1
[accesstime] => 1509042799
[path] => /var/www/foo
[basepath] => /var/www
[group] => 1000
[modificationtime] => 1508955700
[readable] => 1
[owner] => 1000
[writable] => 1
)