Skip to content

Core\IO::isReadable

Core\IO::isReadable(string $path): boolCore\IO::isReadable(
    string $path,
): bool

Checks whether the program may read what is at the path.

Core\IO::isReadable returns true when the operating system allows this program to read the file or folder at the path. It returns false when it does not. It also returns false when there is nothing at the path, and it does not throw an error for that.

The result is true at the moment of the check. Another program can delete or change the file right after it. So a Core\IO::read that follows can still throw an IOError, and your program should still catch it.

A program you start from the command line may read any path. On a server, a program may read only the files in its application folder, except nvs.toml and names that start with .. Any other path needs the fs.read capability. Without it, isReadable throws a RuntimeError. It does not return false.

Good to know: use isReadable when a missing file is a normal case, such as an optional settings file.

Parameters

  • $pathstringThe file or directory to check. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.

Return value

Returns bool. true if the operating system would allow a read, false if it would not. A path where nothing exists gives false. The result is only true at the moment of the call, so a read after it can still fail.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path.

Examples

A file a folder and nothing

<?nvs
// Shows what `Core\IO::isReadable` returns for a file, a folder and a path with nothing at it.
string $dir = Core\IO::createTemporaryDirectory();
string $file = Core\Path::join($dir, 'notes.txt');
Core\IO::write($file, 'hello');
// The program may read this file. This prints "true".
echo Core\IO::isReadable($file) ? 'true' : 'false', "\n";
// A folder can be readable too. This prints "true".
echo Core\IO::isReadable($dir) ? 'true' : 'false', "\n";
// Nothing has this name. `isReadable` does not throw an error. This prints "false".
echo Core\IO::isReadable(Core\Path::join($dir, 'missing.txt')) ? 'true' : 'false', "\n";
Output
true
true
false

A settings file that may be missing

<?nvs
// Shows how a program reads a settings file only when it can, and uses a default value otherwise.
class Settings
{
public static function greeting(string $path): string
{
// The file is not there yet, so `isReadable` returns `false`.
if (!Core\IO::isReadable($path)) {
return 'Hello';
}
return Core\Str::trim(Core\IO::read($path));
}
}
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'greeting.txt');
// This prints "Hello", the default value.
echo Settings::greeting($path), "\n";
// Now the file exists. This prints "Good morning", the text in the file.
Core\IO::write($path, "Good morning\n");
echo Settings::greeting($path), "\n";
Output
Hello
Good morning

Pick the first config file found

<?nvs
// Shows how a program looks for its configuration in several places and uses the first file it can read.
string $dir = Core\IO::createTemporaryDirectory();
Core\IO::makeDirectory(Core\Path::join($dir, 'etc'));
Core\IO::write(Core\Path::join($dir, 'etc/app.ini'), "mode = production\n");
// The local file comes first, but it does not exist here.
array<string> $candidates = [
Core\Path::join($dir, 'app.local.ini'),
Core\Path::join($dir, 'etc/app.ini'),
];
// `isReadable` is `false` for "app.local.ini" and `true` for "etc/app.ini".
// This prints "using app.ini" and then "mode = production".
foreach ($candidates as string $path) {
if (Core\IO::isReadable($path)) {
echo 'using ', Core\Path::fileName($path), "\n";
echo Core\IO::read($path);
break;
}
}
Output
using app.ini
mode = production