PHP linkinfo() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, linkinfo() checks that a link exists and returns the device number of the disk that holds it.

It is mostly used to confirm that a symbolic link is valid, or to compare two paths and see if they live on the same device.

Syntax

Call linkinfo() with the values you need. Start with this line.

PHP
$device = linkinfo('current');
CallWhat it does
linkinfo('current')Returns the device number of the link named current.
linkinfo('missing-link')Returns -1 with a warning because the link does not exist.
linkinfo('/tmp') === linkinfo('/var/tmp')Compares the devices of two paths.

linkinfo() returns the st_dev field of the lstat call, so it describes the link itself and not the file it points to. The number is only useful for comparing or for checking that the link exists.

Library declarations
PHP
linkinfo(string $path): int|false

Parameters

ParameterDescription
pathRequired. The path to the link you want to inspect.

Return Value

Return ValueDescription
int|falseReturns the device number as a non-negative integer on success. On failure PHP raises a warning and returns -1.

PHP Examples

Example 1 — Check That a Link Exists

Create a symbolic link and use linkinfo() to confirm it can be read.

PHP
<?php
$link = __DIR__ . '/current';
@unlink($link);
symlink('releases/v1', $link);

if (linkinfo($link) !== -1) {
    echo "The link exists", PHP_EOL;
}

How It Works

  1. symlink() creates a link named current.
  2. linkinfo() asks the system about the link and returns a device number.
  3. The number is not -1, so the link is valid.
  4. The message is printed.

Example 2 — A Link That Is Missing

linkinfo() returns -1 when the path cannot be read.

PHP
<?php
$result = @linkinfo(__DIR__ . '/no-such-link');

var_dump($result);

How It Works

  1. The path does not exist on the disk.
  2. linkinfo() raises a warning and returns -1.
  3. The @ symbol hides the warning for this demo.
  4. var_dump() shows the type and value, which is int(-1).

Example 3 — Check If Two Files Share a Device

Hard links only work on the same disk, so compare device numbers before calling link().

PHP
<?php
$a = __DIR__ . '/a.txt';
$b = __DIR__ . '/b.txt';

touch($a);
touch($b);

if (linkinfo($a) === linkinfo($b)) {
    echo "Same device: a hard link is possible", PHP_EOL;
} else {
    echo "Different devices: use symlink() or copy()", PHP_EOL;
}

How It Works

  1. touch() creates both files in the same folder.
  2. linkinfo() returns the device number for each path.
  3. Equal numbers mean both files are on the same disk.
  4. In that case link() can safely create a hard link between them.

Frequently Asked Questions

It returns the device number of a link and shows that the link can be read. If it cannot, it returns -1.
It raises a warning and returns -1. Compare the result using === -1.
No. It reads information about the link itself, like lstat() does.
No. readlink() returns the path that a link points to. linkinfo() returns the device number of the link.
Use it to confirm that a link exists, or to compare two paths before creating a hard link.

Next: lstat()

lstat() returns information about a file or a symbolic link, such as its size, owner, perm.

lstat() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful