Core\IO
Reads, writes, copies and lists files and directories. Each method needs the fs.read or fs.write capability for its path, and throws an error instead of returning false. open gives a Core\IO\File to read or write part of a file.
Core\IO reads or replaces a whole file. read reads it as text. readBytes reads a file that is not text, such as an image, as bytes. write and append take a string or bytes. Every call first checks a capability. The path must be inside a folder that nvs.toml grants as fs.read or fs.write. A read grant does not allow writing. A path the configuration does not grant throws a RuntimeError that names the capability. Your program can catch it and continue. A path the configuration allows but the operating system does not throws an IOError. Examples are a missing file and a directory. The check follows the path through its deepest folder that exists. Write a new file's path from such a folder, as ./out.txt and not out.txt. Otherwise the check finds it not granted.
When nvs.toml does not set fs.read or fs.write, a program started from the command line reads and writes any path. Server work reads its application folder, except nvs.toml and names that start with .. It writes only the temporary folders that Core\IO::createTemporaryDirectory made for it.
stdinStream reads the program's standard input in pieces of bytes, one piece per step of a foreach loop.
list returns the names in one folder. walk gives every file and folder under a folder, one Core\IO\Entry per step of a foreach loop. Both sort the names of a folder by their bytes, so the order is the same on every system. Both leave out every file and folder the program may not read. So server work does not see .env, .git or nvs.toml unless nvs.toml grants them. lines reads a text file one line per step of a foreach loop.
[capabilities.fs]read = ["."]write = ["."]alphabeta<?nvsstring $text = Core\IO::read('data.txt');echo Core\Str::length($text), " bytes\n";
Core\IO::write('./copy.txt', $text . "gamma\n");echo Core\Str::trim(Core\IO::read('./copy.txt')), "\n";
try { Core\IO::read('./missing.txt');} catch (IOError $io) { echo "missing: no such file\n";}try { Core\IO::read('../outside.txt');} catch (RuntimeError $denied) { echo "outside: refused\n";}11 bytesalphabetagammamissing: no such fileoutside: refusedMembers
28 members.
| Member | Signature |
|---|---|
read | read(string $path): string |
readBytes | readBytes(string $path): bytes |
write | write(string $path, string|bytes $content): void |
append | append(string $path, string|bytes $content): void |
writeStream | writeStream(string $path, array<bytes>|Iterable<bytes>|Iterator<bytes> $src, { max?: uint, overwrite?: bool }): void |
exists | exists(string $path): bool |
isFile | isFile(string $path): bool |
isDirectory | isDirectory(string $path): bool |
isReadable | isReadable(string $path): bool |
isWritable | isWritable(string $path): bool |
size | size(string $path): uint |
modifiedAt | modifiedAt(string $path): Core\Time\Instant |
metadata | metadata(string $path): Core\IO\Metadata |
copy | copy(string $from, string $to): void |
move | move(string $from, string $to): void |
remove | remove(string $path): void |
makeDirectory | makeDirectory(string $path): void |
removeDirectory | removeDirectory(string $path): void |
list | list(string $path): array<string> |
walk | walk(string $path, { skip?: array<string> }): Core\IO\Walk |
createTemporaryDirectory | createTemporaryDirectory(): string |
canonicalize | canonicalize(string $path): string |
within | within(string $base, string $path): string |
readText | readText(string $path, { charset?: Core\Charset }): string |
lines | lines(string $path): Core\IO\Lines |
open | open(string $path, Core\IO::FileMode $mode): Core\IO\File |
stdin | stdin(): tainted string |
stdinStream | stdinStream(): Core\IO\StdinStream |
Enums
FileModeWhat an open handle may do. There is no binary or text mode: every handle reads and writes the bytes unchanged.
