Read Symbolic Link and Target Metadata Separately in PHP

Last updated: November 25, 2026.

Use lstat() to inspect a symbolic link itself and stat() to inspect the file it points to. If PHP can read the link entry but cannot reach the target, only the first call can return metadata.

<?php
// Set this to an existing symbolic link in your test directory.
$path = __DIR__ . '/current-report';
$link = lstat($path);
if ($link === false) {
    throw new RuntimeException('Cannot read the path metadata.');
}

$target = stat($path);
echo 'Link entry size: ', $link['size'], PHP_EOL;

if ($target === false) {
    echo 'Target metadata unavailable.', PHP_EOL;
} else {
    echo 'Target size: ', $target['size'], PHP_EOL;
}

Decide which size you need

For a report download, use the target’s size because it describes the bytes the application will read. For an inventory of links, use the entry metadata. On typical Unix filesystems, the link size describes the length in bytes of the stored target path. It does not measure the size of the file at that path. On ordinary files, both functions describe the same entry.

Interpret a failed target lookup

When lstat() succeeds and stat() fails, check the destination stored in the link. A relative destination is resolved from the directory containing the link. A missing file is one possibility, but an inaccessible parent directory or a link loop can also make target lookup fail. PHP can emit a warning when the lookup fails. Run this diagnostic from the CLI or use your application’s normal error logging.

Check explicitly for false before reading the array. A successful result can contain a size of zero. If your script checks the same path again after another process replaces the file or retargets the link, clear the cached filesystem status before repeating the check.

PHP documentation

Related guides

Sergey Kornilov

Sergey Kornilov