Core\IO::metadata
Core\IO::metadata(string $path): Core\IO\MetadataCore\IO::metadata( string $path, ): Core\IO\Metadata
Returns the size, the time of the last change and the kind of a file or folder, with one call.
Core\IO::metadata takes a path and returns a Core\IO\Metadata. This object has four methods. size() returns the size in bytes, and modifiedAt() returns the time of the last change. isFile() returns true for a file, and isDirectory() returns true for a folder.
Core\IO::size, Core\IO::modifiedAt, Core\IO::isFile and Core\IO::isDirectory each return one of these values. When a program needs more than one of them for the same path, metadata is faster, because it calls the operating system only once.
The Core\IO\Metadata object does not change. If the file changes after the call, the object still has the old values. Call metadata again to get the new ones.
metadata throws an IOError when there is nothing at the path.
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, metadata throws a RuntimeError.
Parameters
$pathstringThe file or directory to measure. 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 Core\IO\Metadata. A Core\IO\Metadata. Its values are from the moment of the call. They do not change when the file changes later.
Errors
RuntimeErrorThe configuration does not grantfs.readfor this path.IOErrorNothing is at the path, or its metadata could not be read. A missing file has no metadata, so this method throws an error.existsreturnsfalsefor it.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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
Size and kind of a file
<?nvs// Shows how to read the size of a file and check if it is a file or a folder.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'note.txt');Core\IO::write($path, "Café\n");
var $info = Core\IO::metadata($path);
// "é" is two bytes, so the file has 6 bytes. This prints "size: 6".echo 'size: ', $info->size(), "\n";
// This prints "file: true" and "folder: false".echo 'file: ', $info->isFile() ? 'true' : 'false', "\n";echo 'folder: ', $info->isDirectory() ? 'true' : 'false', "\n";size: 6file: truefolder: falseValues do not change
<?nvs// Shows that the result of `Core\IO::metadata` keeps the values from the moment of the call.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'log.txt');Core\IO::write($path, "one\n");var $before = Core\IO::metadata($path);
// The file gets longer after the call. `$before` still has the old size.Core\IO::append($path, "two\n");var $after = Core\IO::metadata($path);
// This prints "before: 4" and "after: 8".echo 'before: ', $before->size(), "\n";echo 'after: ', $after->size(), "\n";
// There is nothing at this path, so `metadata` throws an `IOError`. This prints "not found".try { Core\IO::metadata(Core\Path::join(Core\IO::createTemporaryDirectory(), 'missing.txt'));} catch (IOError $e) { echo 'not found', "\n";}before: 4after: 8not foundList a folder with sizes
<?nvs// Shows how a program lists the files in a folder with the size of each one.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'uploads');Core\IO::makeDirectory(Core\Path::join($dir, 'thumbnails'));Core\IO::write(Core\Path::join($dir, 'contract.pdf'), Core\Str::repeat('x', 2048));Core\IO::write(Core\Path::join($dir, 'notes.txt'), "call back on Monday\n");
// One `metadata` call for each name gives both the kind and the size.// The folder "thumbnails" is not a file, so it is left out.foreach (Core\IO::list($dir) as string $name) { var $info = Core\IO::metadata(Core\Path::join($dir, $name)); if ($info->isFile()) { echo $name, ': ', $info->size(), ' bytes', "\n"; }}contract.pdf: 2048 bytesnotes.txt: 20 bytes