Core\IO\Entry::isLink
$entry->isLink(): boolChecks whether an entry that Core\IO::walk found is a symbolic link or a junction.
A symbolic link is a name that points to another file or folder. A junction is a Windows link to a folder. $entry->isLink() returns true for a link. Then isFile() and isDirectory() both return false, even when the link points to a folder. For a regular file or a folder, isLink() returns false.
walk never follows a link. The link itself is in the result, but the files behind it are not. This means a link cannot make a walk leave the start folder, or go around in a circle. path() is the path of the link itself. Pass it to a method such as Core\IO::readText to read the file the link points to.
The examples below check the entries of an ordinary tree, walk a tree that has a link to a folder, and sort each entry into a file, a folder or a link.
Return value
Returns bool. true for a link. Then isFile() and isDirectory() both give false.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\IO::walk gives a Core\IO\Entry for each file and folder. path(), relativePath() and name() give the full path, the path from the start folder and the last part. isFile(), isDirectory() and isLink() tell you what the entry is. |
Examples
An ordinary tree
<?nvs// Shows that regular files and folders are not links.
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');Core\IO::makeDirectory(Core\Path::join($photos, '2024'));Core\IO::write(Core\Path::join($photos, '2024', 'beach.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, 'notes.txt'), 'notes');
// This tree has no links, so this prints "false" for each of the three entries.foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { echo $entry->name(), ': ', $entry->isLink() ? 'true' : 'false', "\n";}2024: falsebeach.jpg: falsenotes.txt: falseA link is not followed
<?nvs// Shows that `Core\IO::walk` gives a link as one entry and does not go into it.
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');string $year = Core\Path::join($photos, '2025');string $latest = Core\Path::join($photos, 'latest');Core\IO::makeDirectory($year);Core\IO::write(Core\Path::join($year, 'forest.jpg'), 'jpg');
// "latest" is a link to the folder "2025". On Windows, `mklink /J` makes a junction,// which is a Windows link to a folder. Other systems use `ln -s`.if (Core\Path::SEPARATOR == '\\') { Core\Process::run('C:\\Windows\\System32\\cmd.exe', ['/C', 'mklink', '/J', $latest, $year]);} else { Core\Process::run('/bin/ln', ['-s', $year, $latest]);}
// For a link, `isFile()` and `isDirectory()` both return `false`, even for a link to a folder.// The walk does not go into "latest", so "forest.jpg" is printed only once.foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { string $kind = $entry->isLink() ? 'link' : ($entry->isDirectory() ? 'folder' : 'file'); echo Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), ': ', $kind, "\n";}2025: folder2025/forest.jpg: filelatest: linkSort entries by kind
<?nvs// Shows how a backup program decides what to do with each entry of a folder tree.
class Backup{ // `action` returns what the backup does with the entry. A link is not a file or a folder, // so the method checks for a link first. public static function action(Core\IO\Entry $entry): string { if ($entry->isLink()) { return 'skip the link'; } if ($entry->isDirectory()) { return 'make the folder'; } if ($entry->isFile()) { return 'copy the file'; } // A device or a named pipe is not a link, a folder or a file. return 'skip'; }}
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');Core\IO::makeDirectory(Core\Path::join($photos, '2024'));Core\IO::write(Core\Path::join($photos, '2024', 'beach.jpg'), 'jpg');
// This tree has no links. This prints "make the folder" for "2024"// and "copy the file" for "beach.jpg".foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { echo $entry->name(), ': ', Backup::action($entry), "\n";}2024: make the folderbeach.jpg: copy the file