PHP readlink function tutorial shows how to read symbolic link targets in PHP. Learn readlink with practical examples.
last modified April 3, 2025
The PHP readlink function returns the target of a symbolic link. It’s essential for working with symlinks in filesystem operations.
The readlink function returns the target path of a symbolic link. It takes one parameter: the path to the symbolic link.
Syntax: readlink(string $path): string|false. The function returns the target path or false on failure. It only works on symbolic links.
This shows the simplest usage of readlink to get a symlink target.
basic_readlink.php
<?php
declare(strict_types=1);
$linkPath = “/var/www/html/link-to-config”; $target = readlink($linkPath);
if ($target !== false) { echo “Symlink points to: " . $target; } else { echo “Failed to read symlink or not a symlink”; }
This reads the target of a symbolic link. Always check the return value as it may fail if the path isn’t a symlink or doesn’t exist.
readlink may return relative paths that need resolution.
relative_path.php
<?php
declare(strict_types=1);
$linkPath = “data/link-to-docs”; $target = readlink($linkPath);
if ($target !== false) { $absolutePath = realpath(dirname($linkPath) . ‘/’ . $target; echo “Absolute path: " . $absolutePath; } else { echo “Failed to read symlink”; }
This example shows how to resolve relative symlink targets to absolute paths. The realpath function helps get the canonicalized absolute path.
It’s good practice to verify a path is a symlink before reading it.
check_before_read.php
<?php
declare(strict_types=1);
$path = “/usr/bin/php”;
if (is_link($path)) { $target = readlink($path); echo “Symlink target: " . $target; } else { echo “Path is not a symbolic link”; }
Here we use is_link to check if the path is a symlink before attempting to read it. This prevents errors when dealing with regular files.
Proper error handling is important when working with filesystem functions.
error_handling.php
<?php
declare(strict_types=1);
$linkPath = “/tmp/nonexistent-link”;
try { $target = readlink($linkPath);
if ($target === false) {
throw new RuntimeException("Failed to read symlink");
}
echo "Symlink target: " . $target;
} catch (Throwable $e) { echo “Error: " . $e->getMessage(); }
This demonstrates robust error handling for symlink operations. The try-catch block captures any filesystem-related exceptions that might occur.
This example shows how to resolve symlinks that point to other symlinks.
recursive_resolve.php
<?php
declare(strict_types=1);
function resolveSymlink(string $path, int $maxDepth = 10): string { if ($maxDepth <= 0) { throw new RuntimeException(“Maximum symlink depth exceeded”); }
if (!is_link($path)) {
return $path;
}
$target = readlink($path);
if ($target === false) {
throw new RuntimeException("Failed to read symlink");
}
if (str_starts_with($target, '/')) {
return resolveSymlink($target, $maxDepth - 1);
}
$dir = dirname($path);
return resolveSymlink($dir . '/' . $target, $maxDepth - 1);
}
$finalPath = resolveSymlink(’/var/www/html/link-to-app’); echo “Final path: " . $finalPath;
This recursive function resolves symlinks until it reaches a regular file or directory. It includes safety checks for maximum recursion depth and relative path resolution.
Check First: Verify paths exist and are symlinks before reading.
Handle Errors: Always check the return value for false.
Path Resolution: Be prepared to handle relative paths.
Security: Validate symlink targets before using them.
Recursion: Handle symlink chains carefully to avoid loops.
This tutorial covered the PHP readlink function with practical examples showing its usage in different scenarios with symbolic links.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.