The File
namespace in the FileManager
package provides a set of functions to perform operations on files.
Here you can see a list of included functions and their documentation.
function chmod(string $path, int $permission): bool
The chmod
function changes the permissions of a file or directory at the specified path.
Note PHP's
umask
does not have any effect on this function.
use function PhpRepos\FileManager\File\chmod;
chmod($filepath, 0666); // drw-rw-rw-
chmod($filepath, 0600); // drw-------
chmod($filepath, 0660); // drwxrwx---
function content(string $path): string
The content
function returns the content of a file at the specified path.
It equals cat path
.
use function PhpRepos\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 PhpRepos\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 PhpRepos\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 PhpRepos\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 PhpRepos\FileManager\File\create;
use function PhpRepos\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 PhpRepos\FileManager\File\create;
use function PhpRepos\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 PhpRepos\FileManager\File\create;
use function PhpRepos\FileManager\File\content;
use function PhpRepos\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 PhpRepos\FileManager\File\move;
move('path-to-file', 'path-to-destination');
function permission(string $path): int
It returns the file permission.
use function PhpRepos\FileManager\File\create;
use function PhpRepos\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 PhpRepos\FileManager\File\create;
use function PhpRepos\FileManager\File\permission;
use function PhpRepos\FileManager\File\preserve_copy;
create('/root/home/file1', '', 0666);
preserve_copy('/root/home/file1', '/root/home/file2');
echo permission('/root/home/file2'); // Output 0666