Core\IO::exists
Checks whether a file or a folder exists at a path.
Core\IO::exists returns true when the path names a file or a folder. It returns false when there is nothing at the path. A missing file is a normal result here, so exists does not throw an error for 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, exists throws a RuntimeError. It does not return false, so false always means that nothing is there.
Good to know: to find out what kind of thing is at the path, use Core\IO::isFile or Core\IO::isDirectory.
Parameters
$pathstringThe name to look for. 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 something is at the path, false if nothing is there. A missing path is a normal result here and not an error. size throws an error for it.
Errors
RuntimeErrorThe configuration does not grantfs.readfor this path. This error is different fromfalse. A program without access to a folder cannot use this method to learn what is in it.IOErrorThe operating system could not say whether the path exists. For example, the process may not open a parent directory. That is not the same as a missing path.
Examples
A file a folder and nothing
<?nvs// Shows what `Core\IO::exists` 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');
// A file exists. This prints "true".echo Core\IO::exists($file) ? 'true' : 'false', "\n";
// A folder exists too. This prints "true".echo Core\IO::exists($dir) ? 'true' : 'false', "\n";
// Nothing has this name. This prints "false".echo Core\IO::exists(Core\Path::join($dir, 'missing.txt')) ? 'true' : 'false', "\n";truetruefalseWrite a default file only once
<?nvs// Shows how a program creates a file with default content only when the file is not there yet.
string $settings = Core\Path::join(Core\IO::createTemporaryDirectory(), 'settings.ini');
// The program checks the file each time it starts. Here it starts twice.// This prints "first start: created" and then "second start: kept".array<string> $starts = ['first start', 'second start'];foreach ($starts as string $start) { if (Core\IO::exists($settings)) { echo $start, ': kept', "\n"; } else { Core\IO::write($settings, "theme = light\n"); echo $start, ': created', "\n"; }}first start: createdsecond start: keptUse the first settings file found
<?nvs// Shows how a program looks for its settings in several places and uses the first file it finds.
string $dir = Core\IO::createTemporaryDirectory();Core\IO::makeDirectory(Core\Path::join($dir, 'defaults'));Core\IO::write(Core\Path::join($dir, 'defaults/app.ini'), "debug = false\n");
// The local file comes first, so it wins when it is there. Here only the default file exists.array<string> $candidates = [ Core\Path::join($dir, 'local/app.ini'), Core\Path::join($dir, 'defaults/app.ini'),];
string $found = '';foreach ($candidates as string $candidate) { if (Core\IO::exists($candidate)) { $found = $candidate; break; }}
// This prints "defaults/app.ini" and then "debug = false".echo Core\Path::fileName(Core\Path::parent($found)), '/', Core\Path::fileName($found), "\n";echo Core\IO::read($found);defaults/app.inidebug = false