Skip to content

Core\FileTransfer\Connection::exists

$connection->exists(string $path): bool$connection->exists(
    string $path,
): bool

Checks whether a file, a folder or a link exists at a path on a server, and returns true or false.

The path is relative to the block's root, such as inbox/orders.csv. A path that is not there returns false, and no error is thrown. A path with a .. part, one that starts with /, or one with a NUL, CR or LF character throws a RuntimeError before anything is sent.

On SFTP, a link counts even when the file it points to is missing. exists returns true for it. On FTP and FTPS it uses MLST when the server supports it. Otherwise it lists the parent folder. A directory block (a folder on disk that replaces the server) checks that folder.

A file can appear or disappear right after the check. A program that reads the file next should still catch the IOError that readBytes throws for a missing file.

Parameters

  • $pathstringThe path, relative to the block's root, such as in/orders.csv.

Return value

Returns bool. true when something exists at the path, else false.

Errors

  • RuntimeErrorThe path starts with /, has an empty part or a .. part, or has a NUL, CR or LF character.
  • IOErrorThe server does not answer.
  • LogicErrorThe connection is closed.

Changelog

VersionChange
0.0.6Core\FileTransfer\Connection lists, reads, writes, moves and removes files on the server, with the method names of Core\IO. write first writes a temporary .part file and then renames it, so the server never has half a file. readStream reads a file in chunks of at most 64 KiB, so a large file is never in memory at once.

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.
[capabilities.file_transfer]
connect = ["partner"]
# A `directory` block also needs the right to read its folder.
[capabilities.fs]
read = ["partner"]

Check for a file

<?nvs
// Shows how to check whether a file or a folder is on a partner's server.
use Core\FileTransfer;
// `open` takes the name of the block `[file_transfer.partner]` in `nvs.toml`.
var $partner = FileTransfer::open('partner');
// This prints "yes", "no" and "yes". A folder also exists.
echo 'inbox/orders.csv: ', $partner->exists('inbox/orders.csv') ? 'yes' : 'no', "\n";
echo 'inbox/returns.csv: ', $partner->exists('inbox/returns.csv') ? 'yes' : 'no', "\n";
echo 'inbox: ', $partner->exists('inbox') ? 'yes' : 'no', "\n";
Output
inbox/orders.csv: yes
inbox/returns.csv: no
inbox: yes

A path that is not allowed

<?nvs
// Shows the error `exists` throws for a path outside the block's folder.
use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
// The parts of a path are the names between the `/` characters. A part `..` means the folder above.
// `../nvs.toml` is outside the block's folder, so `exists` throws a `RuntimeError` and sends nothing.
try {
$partner->exists('../nvs.toml');
} catch (RuntimeError $notAllowed) {
echo $notAllowed->message, "\n";
}
// A path that starts with `/` throws the same error.
try {
$partner->exists('/inbox/orders.csv');
} catch (RuntimeError $notAllowed) {
echo $notAllowed->message, "\n";
}
Output
Core\FileTransfer\Connection::exists: the path "../nvs.toml" is not allowed: it has a `..` part, and a path cannot leave the block's `root`
Core\FileTransfer\Connection::exists: the path "/inbox/orders.csv" is not allowed: it starts with `/`, and every path is relative to the block's `root`

Read only finished uploads

<?nvs
// Shows a script that reads a partner's file only after the partner has finished uploading it.
use Core\Bytes;
use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
// When the partner has uploaded a file, it adds an empty file with the name plus `.done`.
// Here only `invoices.csv` has one: `invoices.csv.done`.
array<string> $files = ['outbox/invoices.csv', 'outbox/refunds.csv'];
foreach ($files as string $file) {
if (!$partner->exists($file . '.done')) {
echo $file, ": not finished yet\n";
continue;
}
// This prints "outbox/invoices.csv: 21 bytes".
echo $file, ': ', Bytes::length($partner->readBytes($file)), " bytes\n";
}
Output
outbox/invoices.csv: 21 bytes
outbox/refunds.csv: not finished yet