Saeghe is a difficult name to pronounce. Therefore, Saeghe project has been renamed to phpkg.
This website no longer receives updates.
Please visit phpkg website at phpkg.com
The Directory
namespace is part of the FileManager package.
Here you can see a list of included functions and their documentation.
function chmod(string $path, int $permission): bool
It changes the directory's permission.
Note PHP's
umask
does not have any effect on this function.
use function Saeghe\FileManager\Directory\chmod;
chmod($directory_path, 0774); // drwxrwxr--
chmod($directory_path, 0755); // drwxr-xr-x
chmod($directory_path, 0777); // drwxrwxrwx
function clean(string $path): void
It cleans the given directory.
It equals to rm -fR your-directory/*
.
use function Saeghe\FileManager\Directory\clean;
clean('/root/home/user/project/directory'); // Equals to running rm -fR /root/home/user/project/directory/*
function delete(string $path): bool
Attempts to remove the directory named by directory.
It equals to rm your-directory
.
The directory must be empty, and the relevant permissions must permit this.
An E_WARNING level error will be generated on failure.
use function Saeghe\FileManager\Directory\delete;
delete('/root/home/user/project/directory'); // Equals to running rm /root/home/user/project/directory
function delete_recursive(string $path): bool
It deletes the given directory recursively.
It equals to rm -fR your-directory
.
use function Saeghe\FileManager\Directory\delete_recursive;
delete_recursive('/root/home/user/project/directory'); // Equals to running rm /root/home/user/project/directory
function exists(string $path): bool
It checks the given path and returns true if the given path exists, and it is a directory, otherwise returns false.
use function Saeghe\FileManager\Directory\exists;
echo (int) exists('/root/home/user/project/directory'); // Output: 1
echo (int) exists('/root/home/user/project/directory/sub-directory-not-exists'); // Output: 0
echo (int) exists('/root/home/user/project/directory/filename.txt'); // Output: 0
function exists_or_create(string $path): bool
It checks the given path, if the directory exists, and is a directory, returns true. If the directory does not exist, it makes the directory, and returns true. Otherwise, it returns false.
use function Saeghe\FileManager\Directory\exists_or_create;
echo (int) exists_or_create('/root/home/user/project/directory'); // Output: 1
echo (int) exists_or_create('/root/home/user/project/directory/sub-directory-will-get-created'); // Output: 1
echo (int) exists_or_create('/root/home/user/project/directory/filename.txt'); // Output: 0
function is_empty(string $path): bool
It returns true if the given directory is empty.
use function Saeghe\FileManager\Directory\is_empty;
echo (int) is_empty('/root/home/user/project/directory'); // Output: 0
echo (int) is_empty('/root/home/user/project/directory/sub-directory'); // Output: 1
function ls(string $path): bool
It returns a list of directory contents.
It excludes hidden contents.
It equals to ls your-directory
.
use function Saeghe\FileManager\Directory\ls;
ls('/root/home/user/project/directory'); // ['sub-directory']
ls('/root/home/user/project/directory/sub-directory'); // []
function ls_all(string $path): bool
It returns a list of directory contents, including the hidden ones.
It equals to ls -a your-directory
.
use function Saeghe\FileManager\Directory\ls_all;
ls_all('/root/home/user/project/directory'); // ['sub-directory', '.git']
ls_all('/root/home/user/project/directory/sub-directory'); // ['.gitignore']
function make(string $path, int $permission = 0775): bool
It makes a directory in the given path with the given permission. If no permission passes, the permission sets as 0775.
use function Saeghe\FileManager\Directory\exists;
use function Saeghe\FileManager\Directory\make;
make('/root/home/user/project/directory/the-directory-name-you-want-to-create');
echo (int) exists('/root/home/user/project/directory/the-directory-name-you-want-to-create'); // Output: 1
function make_recursive(string $path, int $permission = 0775): bool
It makes a directory recursively in the given path with the given permission. If no permission passes, the permission sets as 0775.
use function Saeghe\FileManager\Directory\exists;
use function Saeghe\FileManager\Directory\make;
make('/root/home/user/project/not-exists/the-directory-name-you-want-to-create');
echo (int) exists('/root/home/user/project/not-exists/the-directory-name-you-want-to-create'); // Output 1
function permission(string $path): int
It returns the directory permission.
use function Saeghe\FileManager\Directory\make;
use function Saeghe\FileManager\Directory\permission;
make('/root/home/user/project/directory', 0777);
echo permission('/root/home/user/project/directory'); // Output 0777
make('/root/home/user/project/directory', 0755);
echo permission('/root/home/user/project/directory'); // Output 0755
function preserve_copy(string $origin, string $destination): bool
It preserves the permission from the given origin and makes the given destination directory with the same permission.
It equals to cp -P origin destination
.
use function Saeghe\FileManager\Directory\make;
use function Saeghe\FileManager\Directory\preserve_copy;
use function Saeghe\FileManager\Directory\permission;
make('/root/home/user/project/directory', 0777);
preserve_copy('/root/home/user/project/directory', '/root/home/user/project2/directory');
echo permission('/root/home/user/project2/directory'); // Output 0777
function preserve_copy(string $origin, string $destination): bool
It copies from the given origin to the given destination by preserving the origin content permissions.
It quals to cp -RP origin destination
.
use function Saeghe\FileManager\Directory\make;
use function Saeghe\FileManager\Directory\preserve_copy;
use function Saeghe\FileManager\Directory\permission;
make('/root/home/user/project/directory', 0777);
File\create('/root/home/user/project/directory/file.txt', 0666);
make('/root/home/user/project2/directory');
preserve_copy_recursive('/root/home/user/project/directory', '/root/home/user/project2/directory');
echo permission('/root/home/user/project2/directory/file.txt'); // Output: 0666
function renew(string $path): void
It makes the directory if does not exist. It cleans the directory by deleting its content when it exists.
use Saeghe\FileManager\File;
use function Saeghe\FileManager\Directory\exists;
use function Saeghe\FileManager\Directory\renew;
use function Saeghe\FileManager\Directory\preserve_copy;
use function Saeghe\FileManager\Directory\permission;
echo (int) exists('/root/home/user/project/directory'); // Output 0
renew('/root/home/user/project/directory');
echo (int) exists('/root/home/user/project/directory'); // Output 1
File\create('/root/home/user/project/directory/file.txt');
renew('/root/home/user/project1/directory');
echo (int) File\exists('/root/home/user/project/directory/file.txt'); // Output 0
function renew_recursive(string $path): void
It makes the directory recursively if not exists. It cleans the directory by deleting its content when it exists.
use Saeghe\FileManager\File;
use function Saeghe\FileManager\Directory\exists;
use function Saeghe\FileManager\Directory\renew;
use function Saeghe\FileManager\Directory\preserve_copy;
use function Saeghe\FileManager\Directory\permission;
echo (int) exists('/root/home/user/project/directory'); // Output 0
renew_recursive('/root/home/user/project/directory/subdirectory');
echo (int) exists('/root/home/user/project/directory/subdirectory'); // Output 1
File\create('/root/home/user/project/directory/subdirectory/file.txt');
renew_recursive('/root/home/user/project1/directory/subdirectory');
echo (int) File\exists('/root/home/user/project/directory/subdirectory/file.txt'); // Output 0