PHP readlink Function
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.
Basic Definition
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.
Basic readlink Example
This shows the simplest usage of readlink to get a symlink target.
<?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.
Relative Path Resolution
readlink may return relative paths that need resolution.
<?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.
Checking Before Reading
It's good practice to verify a path is a symlink before reading it.
<?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.
Error Handling
Proper error handling is important when working with filesystem functions.
<?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.
Recursive Symlink Resolution
This example shows how to resolve symlinks that point to other symlinks.
<?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.
Best Practices
- 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.
Source
This tutorial covered the PHP readlink function with practical
examples showing its usage in different scenarios with symbolic links.