PHP readlink() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, readlink() returns the path that a symbolic link points to. It tells you where the link leads without opening the target.

Use it to find the real location of a link, for example to see which release folder a link named current uses on your server.

Syntax

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

PHP
$target = readlink('current');
CallWhat it does
readlink('current')Returns the path stored inside the link, such as releases/v3.
readlink(__FILE__)Returns false because a normal file is not a symbolic link.
is_link($path) ? readlink($path) : $pathReads the link only when the path really is a link.

readlink() returns the text stored in the link exactly as it was created, so it may be a relative path. To get a full, clean path, use realpath() instead. It does not check whether the target still exists.

Library declarations
PHP
readlink(string $path): string|false

Parameters

ParameterDescription
pathRequired. The path of the symbolic link.

Return Value

Return ValueDescription
string|falseReturns the path the link points to, or false on failure.

PHP Examples

Example 1 — Read Where a Link Points

Create a symbolic link and read the path stored in it.

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

echo "Link points to: ", readlink($link), PHP_EOL;

How It Works

  1. unlink() removes an old link so the script can run again.
  2. symlink() creates current as a link that points to target. txt.
  3. readlink() reads the stored path from the link.
  4. The path is printed exactly as it was saved.

Example 2 — A Path That Is Not a Link

readlink() returns false when the path is a normal file.

PHP
<?php
$result = @readlink(__FILE__);

var_dump($result);

How It Works

  1. __FILE__ is the running script, which is a regular file.
  2. A regular file has no link target to read.
  3. readlink() returns false and raises a warning, which the @ hides here.
  4. Use is_link() first to avoid this problem.

Example 3 — Check Several Paths

Loop over a link and a normal file, and report what each one is.

PHP
<?php
chdir(__DIR__);

file_put_contents('plain.txt', 'data');
@unlink('current');
symlink('releases/v3', 'current');

foreach (['current', 'plain.txt'] as $path) {
    echo $path, ': ';
    echo is_link($path) ? 'points to ' . readlink($path) : 'is not a symlink', PHP_EOL;
}

How It Works

  1. chdir() makes the short relative names work in the current folder.
  2. symlink() creates current, which points to releases/v3.
  3. is_link() tells the loop which paths are real symbolic links.
  4. readlink() is only called for the link, so no warning appears.

Frequently Asked Questions

It returns the path that a symbolic link points to. It does not follow the link or open the target.
readlink() returns the text stored in the link, which may be relative. realpath() returns the full, cleaned absolute path of the final target.
The path is not a symbolic link, or it does not exist, or you do not have permission to read it.
No. It only reads the stored path. Use file_exists() on the link to test whether the target is still there.
It works on modern Windows for symbolic links, but creating links may need administrator rights. Test your code on the system you will use.

Next: realpath()

realpath() turns a messy file path into a clean, absolute one. It removes things like ./ a.

realpath() 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