W3docs

is_link()

The is_link() function is a built-in PHP function that checks whether a given path is a symbolic link. This function returns true if the path is a symbolic

A symbolic link (or symlink) is a special file that points to another file or directory, much like a shortcut. The is_link() function is a built-in PHP function that tells you whether a given path is itself a symbolic link.

It returns:

  • true — if the path exists and is a symbolic link.
  • false — if the path is a regular file or directory, does not exist, or cannot be read.

A key point that trips people up: is_link() looks at the path itself, not at what the link points to. A symlink that points to a regular file is still reported as a link by is_link(), even though is_file() would also return true for the same path (because it follows the link to its target).

This page covers the syntax, a complete runnable example, the most common gotcha (the stat cache), and how is_link() differs from related filesystem checks.

Syntax

is_link(string $filename): bool

$filename is the path to check. The function returns a boolean.

A Complete, Runnable Example

Because a symlink rarely exists at a hard-coded path, the most reliable way to see is_link() work is to create one first with symlink(), check it, then clean up:

<?php

$target = tempnam(sys_get_temp_dir(), 'tgt'); // a real regular file
$link   = sys_get_temp_dir() . '/my_symlink';

// Make sure we start clean, then create the symlink.
@unlink($link);
symlink($target, $link);

var_dump(is_link($link));    // bool(true)  — the path is a symlink
var_dump(is_link($target));  // bool(false) — the target is a regular file
var_dump(is_file($link));    // bool(true)  — is_file() follows the link

// Clean up.
unlink($link);
unlink($target);

is_link($link) is true because the path is the symlink, while is_link($target) is false because the target is an ordinary file. Notice that is_file($link) is true — it resolves the link and tests the target.

Try it Yourself isn't available for this example.

The Stat Cache Gotcha

PHP caches the results of filesystem functions like is_link(), is_file(), and file_exists() for performance. If you create, delete, or replace a symlink during the same script run and then re-check it, you may get a stale result. Call clearstatcache() to force a fresh look:

<?php

$link = sys_get_temp_dir() . '/cache_demo';
@unlink($link);
symlink(__FILE__, $link);

var_dump(is_link($link)); // bool(true)

unlink($link);
clearstatcache();          // without this, the next check may still say true
var_dump(is_link($link)); // bool(false)
FunctionReturns true when the path is…
is_link()a symbolic link (does not follow the link)
is_file()a regular file (follows links to the target)
is_dir()a directory (follows links to the target)
file_exists()the target exists (follows links)

To inspect where a link points, use readlink(); to resolve a path with all symlinks expanded, use realpath().

When Would I Use It?

  • Deploy scripts that flip a current symlink between release directories and need to confirm it is actually a link before replacing it.
  • Backup or sync tools that must decide whether to copy a link itself or the file it points to.
  • Security checks that reject user-supplied paths which sneak in via symlinks pointing outside an allowed directory.

Conclusion

is_link() answers one precise question: is this path a symbolic link? It tests the path itself rather than following it, returns false for missing paths, and shares PHP's stat cache — so call clearstatcache() if you change links mid-script. Pair it with is_file(), readlink(), and realpath() when you need to know what a link points to.

Practice

Practice
What does the is_link() function do in PHP?
What does the is_link() function do in PHP?
Was this page helpful?