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 File
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 file's permission.
Note PHP's
umask
does not have any effect on this function.
use function Saeghe\FileManager\File\chmod;
chmod($filepath, 0666); // drw-rw-rw-
chmod($filepath, 0600); // drw-------
chmod($filepath, 0660); // drwxrwx---
function content(string $path): string
It returns the content of the file as a string.
It equals cat path
.
use function Saeghe\FileManager\File\content;
content('/path-to-file');
function copy(string $origin, string $destination): bool
It copies the given origin file to the given destination file.
It equals to cp origin destination
.
use function Saeghe\FileManager\File\copy;
copy('/path-to-origin', '/path-to-destination');
function create(string $path, string $content, ?int $permission = 0664): bool
It creates the given path file and put the given content as its content. The permission of the file sets as the given permission, if not passed, the permission sets as 0664.
use function Saeghe\FileManager\File\create;
create('/path-to-file', 'hello world', 0666);
function delete(string $path): bool
It deletes the file in the given path.
It equals to rm path-to-file
.
use function Saeghe\FileManager\File\delete;
delete('/path-to-file');
function exists(string $path): bool
It returns true if the given path exists, and it is a file. Otherwise, it returns false.
use function Saeghe\FileManager\File\create;
use function Saeghe\FileManager\File\exists;
echo (int) exists('/path-to-file'); // Output: 0
create('/path-to-file', '');
echo (int) exists('/path-to-file'); // Output: 1
function lines(string $path): \Generator
It returns a generator of the file's lines.
use function Saeghe\FileManager\File\create;
use function Saeghe\FileManager\File\lines;
create('path-to-file', 'First line.' . PHP_EOL . 'Second line.');
$results = [];
foreach (lines($file) as $n => $line) {
$results[$n] = $line;
}
// Results: [0 => 'First line.' . PHP_EOL, 1 => 'Second line.']
function modify(string $path, string $content): bool
It modifies the content of the file in the given path with the given content.
use function Saeghe\FileManager\File\create;
use function Saeghe\FileManager\File\content;
use function Saeghe\FileManager\File\modify;
create('path-to-file', 'create content');
modify('path-to-file', 'modified content');
echo content('path-to-file'); // Output: 'modified content'
function move(string $origin, string $destination): bool
It moves the given origin file to the given destination.
It equals to mv origin destination
.
use function Saeghe\FileManager\File\move;
move('path-to-file', 'path-to-destination');
function permission(string $path): int
It returns the file permission.
use function Saeghe\FileManager\File\create;
use function Saeghe\FileManager\File\permission;
create('/root/home/file', '', 0666);
echo permission('/root/home/file'); // Output 0666
create('/root/home/file', '', 0600);
echo permission('/root/home/file'); // Output 0600
function preserve_copy(string $origin, string $destination): bool
It copies the given origin to the given destination and sets the destination's permission as the origin's permission.
It equals to cp -P origin destination
.
use function Saeghe\FileManager\File\create;
use function Saeghe\FileManager\File\permission;
use function Saeghe\FileManager\File\preserve_copy;
create('/root/home/file1', '', 0666);
preserve_copy('/root/home/file1', '/root/home/file2');
echo permission('/root/home/file2'); // Output 0666