Skip to content

Core\FileTransfer\Entry::isFile

$entry->isFile(): bool

Checks whether an entry that list, walk or metadata gave is a file.

$entry->isFile() returns true for a file. It returns false for a folder and for a symbolic link, even when the link points to a file. A symbolic link is a name that points to another file or folder. Use isDirectory() to check for a folder, and isLink() to check for a link.

Only a file has a size, so check isFile() before you add up sizes or download a file. For a folder, size() returns null.

The examples below check each entry in a folder, count the files and the folders, and add up the sizes of the files in a folder.

Return value

Returns bool. true for a file. A link is not a file.

Changelog

VersionChange
0.0.6list 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.

nvs.toml
# 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 its folder.
[capabilities.file_transfer]
connect = ["partner"]
[capabilities.fs]
read = ["partner"]

Files and folders

<?nvs
// Shows which entries in a folder on the server are files.
var $partner = Core\FileTransfer::open('partner');
// "orders.csv" is a file and "reports" is a folder.
// This prints "orders.csv: file" and "reports: not a file".
foreach ($partner->list() as Core\FileTransfer\Entry $entry) {
echo $entry->name(), ': ', $entry->isFile() ? 'file' : 'not a file', "\n";
}
Output
orders.csv: file
reports: not a file

Count the files

<?nvs
// Shows how to count the files and the folders on the server.
var $partner = Core\FileTransfer::open('partner');
uint $files = 0;
uint $folders = 0;
foreach ($partner->walk() as Core\FileTransfer\Entry $entry) {
if ($entry->isFile()) {
$files = $files + 1;
} elseif ($entry->isDirectory()) {
$folders = $folders + 1;
}
}
// This prints "4 files and 2 folders".
echo $files, ' files and ', $folders, ' folders', "\n";
Output
4 files and 2 folders

Add up the file sizes

<?nvs
// Shows how a program adds up the sizes of the files in a folder before it downloads them.
var $partner = Core\FileTransfer::open('partner');
uint $total = 0;
uint $files = 0;
foreach ($partner->walk('reports') as Core\FileTransfer\Entry $entry) {
// Only a file has a size, so the folder "archive" is skipped.
if ($entry->isFile()) {
uint $size = $entry->size() ?? 0;
$total = $total + $size;
$files = $files + 1;
}
}
// The three reports have 25, 25 and 24 bytes. This prints "3 files, 74 bytes".
echo $files, ' files, ', $total, ' bytes', "\n";
Output
3 files, 74 bytes