Skip to content

Core\FileTransfer\Connection::setModifiedAt

$connection->setModifiedAt(string $path, Core\Time\Instant $at): void$connection->setModifiedAt(
    string $path,
    Core\Time\Instant $at,
): void

Sets the modified time of a file on the server. An upload gives a file the current time. Use setModifiedAt to give it the time of the original file instead.

Over SFTP, setModifiedAt also sets the access time of the file to the same time. SFTP can set a time from 1970 to 2106, in whole seconds. Any other time throws an IOError. FTP and FTPS send the MFMT command, also in whole seconds. When the server does not have this command, setModifiedAt throws an IOError that names MFMT.

A directory block changes the time of a file. For a folder, it throws an IOError. A time that the system cannot store throws an IOError. On Windows, the earliest time is in the year 1601.

setModifiedAt throws an IOError when the file does not exist or the server does not allow the change. 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.

metadata($path)->modifiedAt() returns the modified time of a file.

Parameters

  • $pathstringThe path, relative to the block's root, such as in/orders.csv.
  • $atCore\Time\InstantThe new modified time.

Return value

Returns void. Nothing.

Errors

  • RuntimeErrorThe path starts with /, has an empty part or a .. part, or has a NUL, CR or LF character.
  • IOErrorThe file does not exist, the server does not support setting the time, or the server does not answer. A directory block also throws this error when the path is a folder.
  • 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 and writes 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 needs read and write access to its folder.
[capabilities.fs]
read = ["partner"]
write = ["partner"]

Keep the original time of a file

<?nvs
// Shows how to give an uploaded file the modified time of the original file.
var $partner = Core\FileTransfer::open('partner');
// The upload gives the file the current time as its modified time.
$partner->write('report-01.csv', "id,total\n1,30\n" as bytes);
// The original report was last changed at noon on 1 March 2026.
$partner->setModifiedAt('report-01.csv', Core\Time\Instant::fromIso('2026-03-01T12:00:00Z'));
// This prints "2026-03-01T12:00:00Z".
echo $partner->metadata('report-01.csv')->modifiedAt()?->toIso(), "\n";
$partner->remove('report-01.csv');
Output
2026-03-01T12:00:00Z

Find files older than a date

<?nvs
// Shows how to find the files in a folder that were last changed before a date.
use Core\Time\Instant;
var $partner = Core\FileTransfer::open('partner');
// The program writes three files. Then it sets the modified time of each file to the last day of its month.
$partner->makeDirectory('reports-02');
$partner->write('reports-02/january.csv', "1\n" as bytes);
$partner->write('reports-02/february.csv', "2\n" as bytes);
$partner->write('reports-02/march.csv', "3\n" as bytes);
$partner->setModifiedAt('reports-02/january.csv', Instant::fromIso('2026-01-31T18:00:00Z'));
$partner->setModifiedAt('reports-02/february.csv', Instant::fromIso('2026-02-28T18:00:00Z'));
$partner->setModifiedAt('reports-02/march.csv', Instant::fromIso('2026-03-31T18:00:00Z'));
// `$cutoff` is the date to compare with: midnight at the start of 1 March 2026.
var $cutoff = Instant::fromIso('2026-03-01T00:00:00Z');
// `compareTo` compares two times. It returns a negative number when the first time is earlier.
// This prints "february.csv is older" and "january.csv is older".
foreach ($partner->list('reports-02') as Core\FileTransfer\Entry $entry) {
var $modified = $entry->modifiedAt();
if ($modified != null && $modified->compareTo($cutoff) < 0) {
echo $entry->name(), " is older\n";
}
}
$partner->remove('reports-02/january.csv');
$partner->remove('reports-02/february.csv');
$partner->remove('reports-02/march.csv');
$partner->removeDirectory('reports-02');
Output
february.csv is older
january.csv is older

A file that does not exist throws

<?nvs
// Shows the errors `setModifiedAt` throws for a file that does not exist and for a path outside the folder.
var $partner = Core\FileTransfer::open('partner');
var $noon = Core\Time\Instant::fromIso('2026-03-01T12:00:00Z');
// `missing-03.csv` does not exist, so `setModifiedAt` throws an `IOError`.
try {
$partner->setModifiedAt('missing-03.csv', $noon);
} catch (IOError $missing) {
echo "missing-03.csv does not exist\n";
}
// A path that starts with `/` throws a `RuntimeError`. Nothing is sent to the server.
try {
$partner->setModifiedAt('/readme.txt', $noon);
} catch (RuntimeError $absolute) {
echo $absolute->message, "\n";
}
Output
missing-03.csv does not exist
Core\FileTransfer\Connection::setModifiedAt: the path "/readme.txt" is not allowed: it starts with `/`, and every path is relative to the block's `root`