Core\IO::isFile
Checks whether the path names a file.
Core\IO::isFile returns true when there is a file at the path. It returns false for a folder, and false when there is nothing at the path. It does not throw an error for a missing file.
A symbolic link (a name that points to another file) is followed. So a link to a file returns true.
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, isFile throws a RuntimeError.
Good to know: isFile checks what is at the path, not the name. A folder named report.pdf is still a folder.
Parameters
$pathstringThe path 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 for a regular file. false for a directory, for any other kind of entry, and for a path where nothing exists. A missing path gives false and does not throw an error.
Errors
RuntimeErrorThe configuration does not grantfs.readfor this path. This error is different fromfalse, as it is forexists.IOErrorThe operating system could not say whether the path is a file. For example, the process may not open a parent directory. That is not the same as a missing file.
Examples
A file a folder and nothing
<?nvs// Shows what `Core\IO::isFile` 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. This prints "true".echo Core\IO::isFile($file) ? 'true' : 'false', "\n";
// A folder is not a file. This prints "false".echo Core\IO::isFile($dir) ? 'true' : 'false', "\n";
// Nothing has this name. `isFile` does not throw an error. This prints "false".echo Core\IO::isFile(Core\Path::join($dir, 'missing.txt')) ? 'true' : 'false', "\n";truefalsefalseA folder with a file name
<?nvs// Shows that `Core\IO::isFile` checks what is at the path, not what the name looks like.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'report.pdf');Core\IO::makeDirectory($path);
// Something is at the path, so `exists` returns `true`. This prints "exists: true".echo 'exists: ', Core\IO::exists($path) ? 'true' : 'false', "\n";
// It is a folder, even though its name ends in ".pdf". This prints "isFile: false".echo 'isFile: ', Core\IO::isFile($path) ? 'true' : 'false', "\n";
// Reading a folder throws an error. Check with `isFile` first to avoid it. This prints "not a file".if (Core\IO::isFile($path)) { echo Core\IO::read($path);} else { echo 'not a file', "\n";}exists: trueisFile: falsenot a fileRead only the files in a folder
<?nvs// Shows how a program reads every file in a folder and skips the folders inside it.
string $inbox = Core\Path::join(Core\IO::createTemporaryDirectory(), 'inbox');Core\IO::makeDirectory(Core\Path::join($inbox, 'archive'));Core\IO::write(Core\Path::join($inbox, 'a.txt'), "first message\n");Core\IO::write(Core\Path::join($inbox, 'b.txt'), "second message\n");
// `Core\IO::list` returns "a.txt", "b.txt" and "archive". `isFile` is `false` for "archive",// so the loop skips it. This prints the two messages.foreach (Core\IO::list($inbox) as string $name) { string $path = Core\Path::join($inbox, $name); if (!Core\IO::isFile($path)) { continue; } echo Core\IO::read($path);}first messagesecond message