Core\FileTransfer\Entry::permissions
$entry->permissions(): ?uintReturns the Unix permission bits of a file or folder on the server, such as 0o644.
The bits say who can read, write and run the file. They are written as an octal number (base 8), such as 0o640, with three digits. The first digit is for the owner, the second for the group, and the third for all other users. Each digit adds 4 for read, 2 for write and 1 to run. 0o640 means that the owner can read and write, the group can read, and others can do nothing.
$entry->permissions() returns a uint, or null when the server does not send the bits. Some servers send none. A directory block on Windows also returns null, because Windows has no permission bits. Check for null, or use ?? with a value that is safe for your program.
setPermissions on the connection changes the bits.
The examples below read the bits of a file one digit at a time, check that other users cannot write a file, and copy a file with its permissions.
Return value
Returns ?uint. The bits, or null when the server does not send them. A directory block on Windows returns null.
Changelog
| Version | Change |
|---|---|
0.0.6 | list and walk return one Core\FileTransfer\Entry for each file or folder, and it has the same methods as Core\IO\Entry. Its size, modifiedAt and permissions methods return null when the server does not send the value. Every string it returns is tainted: the server chose it, so the methods of the connection do not accept it until your program checks it. |
Examples
The examples below run with this configuration file.
# The `partner` block reads the folder `partner` beside this file. It needs no server.[file_transfer.partner]directory = "partner"
# The program may open the `partner` block, and read and change the files in its folder.[capabilities.file_transfer]connect = ["partner"]
[capabilities.fs]read = ["partner"]write = ["partner"]Read the bits of a file
<?nvs// Shows the permission bits of a file, as one digit each for the owner, the group and other users.// requires: unix
var $partner = Core\FileTransfer::open('partner');$partner->write('report.csv', "month,total\n2026-03,1410\n" as bytes);$partner->setPermissions('report.csv', 0o640);
// `permissions()` returns `null` when the server does not send the bits. `??` replaces `null` with 0.uint $bits = $partner->metadata('report.csv')->permissions() ?? 0;
// Each digit adds 4 for read, 2 for write and 1 to run the file.// This prints "owner: 6", "group: 4" and "others: 0".echo 'owner: ', ($bits >> 6) & 7, "\n";echo 'group: ', ($bits >> 3) & 7, "\n";echo 'others: ', $bits & 7, "\n";
$partner->remove('report.csv');owner: 6group: 4others: 0Check that others cannot write
<?nvs// Shows how a program checks that other users cannot change a file on the server.
class Upload{ // `0o002` is the bit that lets other users write the file. public static function othersCanWrite(Core\FileTransfer\Entry $entry): bool { // When the server sends no bits, `??` replaces `null` with 0, and this function returns `false`. // A stricter program can return `true` when `permissions()` returns `null`. uint $bits = $entry->permissions() ?? 0; return ($bits & 0o002) != 0; }}
var $partner = Core\FileTransfer::open('partner');$partner->write('prices.csv', "sku,price\nmug,12\n" as bytes);$partner->setPermissions('prices.csv', 0o644);
// With `0o644`, other users can only read the file. This prints "others can write: no".echo 'others can write: ', Upload::othersCanWrite($partner->metadata('prices.csv')) ? 'yes' : 'no', "\n";
$partner->remove('prices.csv');others can write: noCopy a file with its permissions
<?nvs// Shows how a program copies a file on the server and gives the copy the same permissions.
var $partner = Core\FileTransfer::open('partner');var $source = $partner->metadata('orders.csv');$partner->write('orders-backup.csv', $partner->readBytes('orders.csv'));
// When the server sends no bits, the copy gets `0o644`. Then only the owner can write it.uint $mode = $source->permissions() ?? 0o644;$partner->setPermissions('orders-backup.csv', $mode);
// This prints "copied 19 bytes".echo 'copied ', $partner->metadata('orders-backup.csv')->size() ?? 0, ' bytes', "\n";
$partner->remove('orders-backup.csv');copied 19 bytes