Skip to content

Core\IO::isDirectory

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

Checks whether the path names a folder.

Core\IO::isDirectory returns true when there is a folder (a directory) at the path. It returns false for a file, and false when there is nothing at the path. It does not throw an error for a missing folder.

A symbolic link (a name that points to another folder) is followed. So a link to a folder 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, isDirectory throws a RuntimeError.

Good to know: use isDirectory before Core\IO::list, which throws an error for a file.

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 directory. false for a file, for any other kind of entry, and for a path where nothing exists. A path can exist and still give false for both isFile and isDirectory.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path.
  • IOErrorThe operating system could not say whether the path is a directory. For example, the process may not open a parent directory.

Changelog

VersionChange
0.0.6Core\IO writes the word "directory" in full: createTemporaryDirectory, makeDirectory, removeDirectory and isDirectory. Core\IO::metadata returns the Core\IO\Metadata of a file. The old names were temporaryDir, makeDir, removeDir, isDir and stat.

Examples

A folder a file and nothing

<?nvs
// Shows what `Core\IO::isDirectory` returns for a folder, a file 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 folder. This prints "true".
echo Core\IO::isDirectory($dir) ? 'true' : 'false', "\n";
// A file is not a folder. This prints "false".
echo Core\IO::isDirectory($file) ? 'true' : 'false', "\n";
// Nothing has this name. `isDirectory` does not throw an error. This prints "false".
echo Core\IO::isDirectory(Core\Path::join($dir, 'missing')) ? 'true' : 'false', "\n";
Output
true
false
false

A file where a folder should be

<?nvs
// Shows how a program checks that an upload folder really is a folder before it writes into it.
string $uploads = Core\Path::join(Core\IO::createTemporaryDirectory(), 'uploads');
// A file has the name the folder should have.
Core\IO::write($uploads, 'old data');
// `exists` returns `true`, but `isDirectory` returns `false`. This prints "uploads is not a folder".
if (Core\IO::exists($uploads) && !Core\IO::isDirectory($uploads)) {
echo 'uploads is not a folder', "\n";
}
// The program removes the file and creates the folder. This prints "uploads is a folder".
Core\IO::remove($uploads);
Core\IO::makeDirectory($uploads);
if (Core\IO::isDirectory($uploads)) {
echo 'uploads is a folder', "\n";
}
Output
uploads is not a folder
uploads is a folder

Count the files in every folder

<?nvs
// Shows how a program finds the folders inside a folder and counts the files in each one.
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');
Core\IO::makeDirectory(Core\Path::join($photos, '2025'));
Core\IO::makeDirectory(Core\Path::join($photos, '2026'));
Core\IO::write(Core\Path::join($photos, '2025/beach.jpg'), 'x');
Core\IO::write(Core\Path::join($photos, '2026/snow.jpg'), 'x');
Core\IO::write(Core\Path::join($photos, '2026/lake.jpg'), 'x');
Core\IO::write(Core\Path::join($photos, 'index.txt'), 'x');
// `isDirectory` is `true` for "2025" and "2026", and `false` for "index.txt".
// This prints "2025: 1 files" and then "2026: 2 files".
foreach (Core\IO::list($photos) as string $name) {
string $path = Core\Path::join($photos, $name);
if (Core\IO::isDirectory($path)) {
echo $name, ': ', Core\Arr::count(Core\IO::list($path)), ' files', "\n";
}
}
Output
2025: 1 files
2026: 2 files