Skip to content

Core\FileTransfer\Connection::metadata

$connection->metadata(string $path): Core\FileTransfer\Entry$connection->metadata(
    string $path,
): Core\FileTransfer\Entry

Reads the size, the modified time, the permissions and the kind of one file or folder on a server.

It returns a Core\FileTransfer\Entry, the same class that list and walk give. size(), modifiedAt() and permissions() are null when the server does not send that value. When nothing exists at the path, metadata throws an IOError. Use exists when a missing file is a normal case.

A path throws a RuntimeError when it starts with / or has a NUL, CR or LF character. A part of the path between two / that is empty or .. also throws. Then nothing is sent.

On SFTP, the entry of a link describes the link itself, not the file it points to. On FTP and FTPS, metadata uses MLST when the server supports it. Otherwise it lists the parent folder and gets the size and the time with the SIZE and MDTM commands.

Good to know: check the size with metadata before you download a large file. When the file is too large, your program can skip it or read it in parts with readStream.

Parameters

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

Return value

Returns Core\FileTransfer\Entry. The entry. Its relativePath() is the same as its path().

Errors

  • RuntimeErrorThe path starts with /, has an empty part or a .. part, or has a NUL, CR or LF character.
  • IOErrorNothing exists at the path, or the 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"]

The size and kind of a file

<?nvs
// Shows how to read the size and the kind of a file on a partner's server.
use Core\FileTransfer;
// `open` takes the name of a block in `nvs.toml`. The block `[file_transfer.partner]` has the name `partner`.
var $partner = FileTransfer::open('partner');
// `metadata` returns a `Core\FileTransfer\Entry`.
var $orders = $partner->metadata('inbox/orders.csv');
echo $orders->path(), ': ', $orders->size() ?? 0, " bytes\n";
echo 'file: ', $orders->isFile() ? 'yes' : 'no', "\n";
// A folder has an entry too. `inbox/archive` is a folder, so this prints "folder: yes".
var $archive = $partner->metadata('inbox/archive');
echo 'folder: ', $archive->isDirectory() ? 'yes' : 'no', "\n";
Output
inbox/orders.csv: 19 bytes
file: yes
folder: yes

A missing file throws

<?nvs
// Shows the error `metadata` throws when nothing exists at the path.
use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
// `metadata` throws an `IOError` for a missing file.
// Use `exists` when you only need to know whether the file is there.
try {
$partner->metadata('inbox/returns.csv');
} catch (IOError $missing) {
echo $missing->message, "\n";
}
echo 'exists: ', $partner->exists('inbox/returns.csv') ? 'yes' : 'no', "\n";
Output
Core\FileTransfer\Connection::metadata: nothing exists at "inbox/returns.csv"
exists: no

Skip a file that is too large

<?nvs
// Shows an import script that checks the size of each file before it downloads it.
use Core\Bytes;
use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
// The script reads files of up to 100 bytes. A larger file waits for a person to check it.
uint $limit = 100;
array<string> $files = ['inbox/orders.csv', 'inbox/stock.csv'];
foreach ($files as string $file) {
uint $size = $partner->metadata($file)->size() ?? 0;
if ($size > $limit) {
echo $file, ': ', $size, " bytes, too large\n";
continue;
}
echo $file, ': read ', Bytes::length($partner->readBytes($file)), " bytes\n";
}
Output
inbox/orders.csv: read 19 bytes
inbox/stock.csv: 120 bytes, too large