Skip to content

Core\IO\Metadata::isFile

$metadata->isFile(): bool

Checks if a path was a regular file.

$info->isFile() works on the Core\IO\Metadata value that Core\IO::metadata returns. It returns true when the path was a regular file, and false when it was a folder or anything else.

isDirectory checks for a folder. A regular file returns true for isFile and false for isDirectory. Some rare kinds of path, such as a device or a named pipe, return false for both.

The answer is from the moment metadata was called. If the file is later replaced by a folder with the same name, this value still returns true. Call metadata again to check the path again.

Return value

Returns bool. true for a regular file, false for anything else that was there.

Examples

A file or a folder

<?nvs
// Shows how to check if a path is a file, using the result of `Core\IO::metadata`.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'project');
Core\IO::makeDirectory($dir);
string $readme = Core\Path::join($dir, 'README.md');
Core\IO::write($readme, "# Project\n");
// "README.md" is a file. This prints "README.md: true".
echo 'README.md: ', Core\IO::metadata($readme)->isFile() ? 'true' : 'false', "\n";
// "project" is a folder, so it is not a file. This prints "project: false".
echo 'project: ', Core\IO::metadata($dir)->isFile() ? 'true' : 'false', "\n";
Output
README.md: true
project: false

The answer is kept

<?nvs
// Shows that `isFile` returns what was at the path when `Core\IO::metadata` was called.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'cache');
Core\IO::write($path, "old cache\n");
var $before = Core\IO::metadata($path);
// The file is deleted, and a folder with the same name is made.
Core\IO::remove($path);
Core\IO::makeDirectory($path);
var $after = Core\IO::metadata($path);
// `$before` still says it is a file. This prints "before: true" and "after: false".
echo 'before: ', $before->isFile() ? 'true' : 'false', "\n";
echo 'after: ', $after->isFile() ? 'true' : 'false', "\n";
Core\IO::removeDirectory($path);
Output
before: true
after: false

Copy only the files

<?nvs
// Shows how a program copies the files of a folder into a backup folder and skips the folders in it.
string $root = Core\Path::join(Core\IO::createTemporaryDirectory(), 'site');
string $source = Core\Path::join($root, 'public');
string $backup = Core\Path::join($root, 'backup');
Core\IO::makeDirectory(Core\Path::join($source, 'images'));
Core\IO::makeDirectory($backup);
Core\IO::write(Core\Path::join($source, 'index.html'), "<h1>Home</h1>\n");
Core\IO::write(Core\Path::join($source, 'style.css'), "h1 { color: navy; }\n");
// `Core\IO::copy` copies one file. "images" is a folder, so the loop skips it.
// This prints "skipped images", "copied index.html" and "copied style.css".
foreach (Core\IO::list($source) as string $name) {
string $from = Core\Path::join($source, $name);
if (Core\IO::metadata($from)->isFile()) {
Core\IO::copy($from, Core\Path::join($backup, $name));
echo 'copied ', $name, "\n";
} else {
echo 'skipped ', $name, "\n";
}
}
Output
skipped images
copied index.html
copied style.css