Core\FileTransfer\Connection::remove
Deletes one file on the server. To delete a folder, use removeDirectory.
The way the file is deleted depends on the kind of block:
- An SFTP block sends the
REMOVEcommand to the server. - An FTP or FTPS block sends the
DELEcommand to the server. - A
directoryblock deletes the file in its folder on disk.
remove throws an IOError when the file does not exist, when the path is a folder, or when the server does not allow the deletion. It also throws an IOError when the server does not answer. In each case, nothing is deleted.
The path is checked before anything is sent to the server. The path throws a RuntimeError when it starts with /, or has an empty part or a .. part. A part is the text between two /. A NUL, CR or LF character in the path also throws a RuntimeError. An empty path throws a RuntimeError too, because the path must name a file inside the folder of the block.
A directory block needs the fs.write capability for its folder. Without it, remove throws a RuntimeError.
Parameters
$pathstringThe path, relative to the block'sroot, such asin/orders.csv.
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 path is a folder, the server does not allow the deletion, 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"]Delete a file after it is read
<?nvs// Shows how to delete a file from a server once the program has read it.
var $partner = Core\FileTransfer::open('partner');$partner->write('order-01.csv', "id,total\n1,30\n" as bytes);
// The program reads the order, then deletes the file.echo $partner->readBytes('order-01.csv') as string;$partner->remove('order-01.csv');
// This prints "order-01.csv: no".echo 'order-01.csv: ', $partner->exists('order-01.csv') ? 'yes' : 'no', "\n";id,total1,30order-01.csv: noErrors that remove throws
<?nvs// Shows that `remove` throws an `IOError` for a file that does not exist and for a folder.
var $partner = Core\FileTransfer::open('partner');
// `missing-02.csv` does not exist.try { $partner->remove('missing-02.csv');} catch (IOError $missing) { echo "missing-02.csv does not exist\n";}
// `folder-02` is a folder. `remove` deletes files only.$partner->makeDirectory('folder-02');try { $partner->remove('folder-02');} catch (IOError $folder) { echo "folder-02 is a folder\n";}
// `removeDirectory` deletes an empty folder.$partner->removeDirectory('folder-02');echo 'folder-02: ', $partner->exists('folder-02') ? 'yes' : 'no', "\n";missing-02.csv does not existfolder-02 is a folderfolder-02: noDelete every file in a folder
<?nvs// Shows how to delete every file in a folder on a server.
var $partner = Core\FileTransfer::open('partner');$partner->makeDirectory('sent-03');$partner->write('sent-03/a.csv', "1\n" as bytes);$partner->write('sent-03/b.csv', "2\n" as bytes);$partner->write('sent-03/c.csv', "3\n" as bytes);
// `list` gives one entry for each file. Its `path()` is tainted, because the server chose the name.// A tainted value comes from outside the program, and it must be checked before some uses.foreach ($partner->list('sent-03') as Core\FileTransfer\Entry $entry) { if ($entry->isFile()) { // `remove` checks the path itself before it sends it to the server. // So `Core\Taint::assertTrusted` can safely turn the path into a normal `string` here. string $path = Core\Taint::assertTrusted($entry->path(), 'remove checks the path first'); $partner->remove($path); echo 'deleted ', $path, "\n"; }}
// The folder is empty now, so `removeDirectory` can delete it. This prints "sent-03: no".$partner->removeDirectory('sent-03');echo 'sent-03: ', $partner->exists('sent-03') ? 'yes' : 'no', "\n";deleted sent-03/a.csvdeleted sent-03/b.csvdeleted sent-03/c.csvsent-03: no