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 PhpRepos\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 PhpRepos\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 PhpRepos\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 PhpRepos\FileManager\Directory\delete_recursive;
delete_recursive('/root/home/user/project/directory'); // Equals to running rm -fR /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 PhpRepos\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 PhpRepos\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 PhpRepos\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): FilesystemCollection
It returns a path list of directory contents. It excludes hidden contents.
use function PhpRepos\FileManager\Directory\ls;
$directory = Path::from_string(root() . 'Tests/PlayGround/Directory');
Directory\make($directory);
Directory\make($directory->append('sub-directory'));
File\create($directory->append('sample.txt'), '');
File\create($directory->append('.hidden.txt'), '');
assert_true(
[
$directory->append('sample.txt'),
$directory->append('sub-directory'),
] == Directory\ls($directory)->items()
);
function ls_all(string $path): FilesystemCollection
It returns a path list of directory contents, including the hidden ones.
use function PhpRepos\FileManager\Directory\ls_all;
$directory = Path::from_string(root() . 'Tests/PlayGround/Directory');
Directory\make($directory);
Directory\make($directory->append('sub-directory'));
File\create($directory->append('sample.txt'), '');
File\create($directory->append('.hidden.txt'), '');
assert_true(
[
$directory->append('.hidden.txt'),
$directory->append('sample.txt'),
$directory->append('sub-directory'),
] == Directory\ls_all($directory)->items()
);
function ls_recursively(string $path): FilesystemTree
It returns a filesystem tree of directory contents, recursively, including the hidden ones.
use function PhpRepos\FileManager\Directory\ls_all;
$playground = Path::from_string(root() . 'Tests/PlayGround');
Directory\make($directory = $playground->append('directory'));
Directory\make($subdirectory = $playground->append('directory/subdirectory'));
File\create($file1 = $playground->append('directory/file1.txt'), '');
Symlink\link($symlink = $playground->append('directory/file1.txt'), $playground->append('directory/symlink'));
File\create($file2 = $playground->append('directory/subdirectory/file2.txt'), '');
File\create($file3 = $playground->append('directory/subdirectory/file3.txt'), '');
Directory\make($hidden_directory = $playground->append('directory/subdirectory/.hidden_directory'));
File\create($hidden_file = $playground->append('directory/subdirectory/.hidden_directory/.hidden_file'), '');
assert_true(
[
$directory,
$file1,
$subdirectory,
$hidden_directory,
$hidden_file,
$file2,
$file3,
$symlink,
] == $tree->vertices()->items()
);
assert_true(
[
new Pair($directory, $file1),
new Pair($directory, $subdirectory),
new Pair($subdirectory, $hidden_directory),
new Pair($hidden_directory, $hidden_file),
new Pair($subdirectory, $file2),
new Pair($subdirectory, $file3),
new Pair($directory, $symlink),
] == $tree->edges()->items()
);
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 PhpRepos\FileManager\Directory\exists;
use function PhpRepos\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 PhpRepos\FileManager\Directory\exists;
use function PhpRepos\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 PhpRepos\FileManager\Directory\make;
use function PhpRepos\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 PhpRepos\FileManager\Directory\make;
use function PhpRepos\FileManager\Directory\preserve_copy;
use function PhpRepos\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 equals to cp -RP origin destination
.
use function PhpRepos\FileManager\Directory\make;
use function PhpRepos\FileManager\Directory\preserve_copy;
use function PhpRepos\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 it does not exist. It cleans the directory by deleting its content when it exists.
use PhpRepos\FileManager\File;
use function PhpRepos\FileManager\Directory\exists;
use function PhpRepos\FileManager\Directory\renew;
use function PhpRepos\FileManager\Directory\preserve_copy;
use function PhpRepos\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 PhpRepos\FileManager\File;
use function PhpRepos\FileManager\Directory\exists;
use function PhpRepos\FileManager\Directory\renew;
use function PhpRepos\FileManager\Directory\preserve_copy;
use function PhpRepos\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