Skip to content

Core\FileTransfer\Connection::move

$connection->move(string $from, string $to): void$connection->move(
    string $from,
    string $to,
): void

Renames a file or a folder on the server. The new path can be in another folder, so move also moves files between folders.

When a file already exists at the new path, move replaces it. SFTP does this in one step when the server has the posix-rename@openssh.com extension. FTP and FTPS send RNFR and RNTO. Some servers do not rename a file to a path where another file exists. On such a server, move first deletes the file at the new path. Then it renames the file again.

A common use is to upload a file under a temporary name, then rename it. A partner who reads the folder then sees only complete files. write already does this for you.

move throws an IOError when nothing exists at the old path, or when the folder of the new path does not exist.

A part of a path is the text between two /. A path throws a RuntimeError when it starts with /, has an empty part or a .. part, or has a NUL, CR or LF character. Both paths are checked before anything is sent.

Parameters

  • $fromstringThe path of the file or folder to rename.
  • $tostringIts new path. The folder of the new path must exist.

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.
  • IOErrorNothing exists at $from, the folder of $to does not exist, 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 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"]

Move a processed file into another folder

<?nvs
// Shows how to move a file into another folder once the program has read it.
var $partner = Core\FileTransfer::open('partner');
$partner->makeDirectory('inbox-01');
$partner->makeDirectory('done-01');
$partner->write('inbox-01/order-7.csv', "id,total\n7,30\n" as bytes);
// The program reads the order, then moves the file to `done-01`.
echo $partner->readBytes('inbox-01/order-7.csv') as string;
$partner->move('inbox-01/order-7.csv', 'done-01/order-7.csv');
// The file is now only in the new folder. This prints "inbox-01: no" and "done-01: yes".
echo 'inbox-01: ', $partner->exists('inbox-01/order-7.csv') ? 'yes' : 'no', "\n";
echo 'done-01: ', $partner->exists('done-01/order-7.csv') ? 'yes' : 'no', "\n";
// This deletes the file and the folders, so the example can run again.
$partner->remove('done-01/order-7.csv');
$partner->removeDirectory('done-01');
$partner->removeDirectory('inbox-01');
Output
id,total
7,30
inbox-01: no
done-01: yes

Move replaces a file that exists

<?nvs
// Shows that `move` replaces a file that already exists at the new path.
var $partner = Core\FileTransfer::open('partner');
$partner->write('prices-02.csv', "id,price\n1,10\n" as bytes);
$partner->write('prices-02.new', "id,price\n1,12\n" as bytes);
// `prices-02.csv` exists, and `move` replaces it with the new file.
$partner->move('prices-02.new', 'prices-02.csv');
// This prints the new prices.
echo $partner->readBytes('prices-02.csv') as string;
// This prints "prices-02.new: no".
echo 'prices-02.new: ', $partner->exists('prices-02.new') ? 'yes' : 'no', "\n";
$partner->remove('prices-02.csv');
Output
id,price
1,12
prices-02.new: no

Errors that move throws

<?nvs
// Shows the errors `move` throws for a path outside the folder and for a file that does not exist.
var $partner = Core\FileTransfer::open('partner');
// Every path must stay inside the folder of the block. `../readme.txt` has a `..` part,
// so `move` throws a `RuntimeError` and this prints its message. Nothing is sent to the server.
try {
$partner->move('readme.txt', '../readme.txt');
} catch (RuntimeError $outside) {
echo $outside->message, "\n";
}
// `missing-03.csv` does not exist, so `move` throws an `IOError`.
try {
$partner->move('missing-03.csv', 'found-03.csv');
} catch (IOError $missing) {
echo "missing-03.csv does not exist\n";
}
Output
Core\FileTransfer\Connection::move: the path "../readme.txt" is not allowed: it has a `..` part, and a path cannot leave the block's `root`
missing-03.csv does not exist