Core\FileTransfer\Connection::makeDirectory
Creates a folder on the server. It also creates each folder above it that does not exist. A folder that already exists is not an error, so you can call makeDirectory before each write.
SFTP sends MKDIR, and FTP and FTPS send MKD. makeDirectory first sends one command for the whole path. When a folder above the last one is missing, that command fails. Then makeDirectory creates each folder of the path in turn, starting nearest the block's root. A directory block creates the folders inside its folder.
makeDirectory throws an IOError when a file exists at the path or at a folder above it. It also throws an IOError when the server does not allow it or does not answer.
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. An empty path throws too, because the folder of the block always exists. Then nothing is sent.
A directory block needs the fs.write capability for its folder. Without it, makeDirectory throws a RuntimeError.
Parameters
$pathstringThe path of the folder, relative to the block'sroot.
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.IOErrorA file exists at the path or above it, the server does not allow it, or the server does not answer.LogicErrorThe connection is closed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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.
# 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"]Create a folder and the folders above it
<?nvs// Shows that `makeDirectory` creates a folder and each folder above it that does not exist.
var $partner = Core\FileTransfer::open('partner');
// Only `partner` exists. `makeDirectory` creates `archive-01`, then `2026`, then `03`.$partner->makeDirectory('archive-01/2026/03');
// This prints "archive-01: yes", "archive-01/2026: yes" and "archive-01/2026/03: yes".array<string> $folders = ['archive-01', 'archive-01/2026', 'archive-01/2026/03'];foreach ($folders as string $folder) { echo $folder, ': ', $partner->exists($folder) ? 'yes' : 'no', "\n";}
// This deletes the folders again, the deepest first.$partner->removeDirectory('archive-01/2026/03');$partner->removeDirectory('archive-01/2026');$partner->removeDirectory('archive-01');archive-01: yesarchive-01/2026: yesarchive-01/2026/03: yesA folder that exists is not an error
<?nvs// Shows that a program can call `makeDirectory` before each write, even when the folder exists.
var $partner = Core\FileTransfer::open('partner');array<string> $orders = ['7', '8', '9'];
foreach ($orders as string $id) { // The first call creates `orders-02`. The next calls find it and do nothing. $partner->makeDirectory('orders-02'); $partner->write('orders-02/' . $id . '.csv', "id\n" as bytes);}
// This prints "3 files".uint $count = 0;foreach ($partner->list('orders-02') as Core\FileTransfer\Entry $entry) { $count = $count + 1;}echo $count, " files\n";
foreach ($orders as string $id) { $partner->remove('orders-02/' . $id . '.csv');}$partner->removeDirectory('orders-02');3 filesA file in the way throws
<?nvs// Shows that `makeDirectory` throws an `IOError` when a file has the name of a folder in the path.
var $partner = Core\FileTransfer::open('partner');
// `notes-03` is a file, so it cannot have a folder inside it.$partner->write('notes-03', "a file\n" as bytes);try { $partner->makeDirectory('notes-03/old'); echo "created\n";} catch (IOError $inTheWay) { echo "notes-03 is a file\n";}
// `makeDirectory` did not change the file. `readBytes` returns its content, so this prints "a file".echo $partner->readBytes('notes-03') as string;
$partner->remove('notes-03');notes-03 is a filea file