Skip to content

lstat()

What is the lstat() Function?

The lstat() function returns metadata about a symbolic link. Unlike stat(), which follows the link and returns information about the target file, lstat() returns information about the link itself.

Here's the basic syntax of the lstat() function:

The PHP syntax of lstat()

php
array lstat(string $filename): array|false

Where $filename is the path to the symbolic link.

How to Use the lstat() Function?

Using the lstat() function is straightforward. Here are the steps to follow:

  1. Specify the path to the symbolic link you want to inspect.
  2. Call the lstat() function, passing the path as a parameter.
  3. Check the return value, as the function returns false on failure.

Here's an example code snippet that demonstrates how to use the lstat() function:

How to Use the lstat() Function?

php
<?php

$link = '/path/to/symbolic/link';
$info = lstat($link);

if ($info === false) {
    echo "Failed to get information about the symbolic link.";
} else {
    echo "The link has a size of {$info['size']} bytes.";
}

The function returns an associative array containing the same keys as stat(): dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, and blocks. Accessing these by name (e.g., `$info['size']`) is safer and more readable than using numeric indices.

Note: Ensure your PHP process has read permissions for the directory containing the symbolic link. On some systems, accessing symlink metadata may require elevated privileges or specific security module configurations.

Conclusion

The lstat() function is a useful tool in PHP for getting information about symbolic links on a file system. By following the steps outlined in this guide, you can easily use the lstat() function in your PHP projects to get information about symbolic links. We hope this guide has been helpful.

Practice

What is the function of lstat() in PHP?

Dual-run preview — compare with live Symfony routes.