What is PHP is_link() Function?
If you want to check whether a filename is a symbolic link or not.
What is a symbolic link? To easily and quickly access files and folders located in different places in your file system easily, we use symbolic link and we use symlink() function to create symbolic link. You can think of this similar to Windows shortcut.
Syntax:
is_link(filename)
Parameters:
The Function has 1 parameter which is required –
filename (Required): it is the name of the symbolic link.
Return Values:
The function returns-
- TRUE – if the parameter is a symbolic link.
- FALSE – if it is not a symbolic link.
Examples:
Example 1:
<?php
$target = "test.txt";
$link = "demo";
symlink($target, $link);
if (is_link($link)) {
echo 'This is a symbolic link';
} else {
echo 'This is not a symbolic link';
}
?>
Output:
This is a symbolic link.
Explanation:
We create a symbolic link “demo” of the file “test.txt”. That’s why the is_link() function returns TRUE and prints “This is a symbolic link”.
Example 2:
<?php
$target = "test.txt";
$link = "demo";
symlink($target, $link);
if (is_link($target)) {
echo 'This is a symbolic link';
} else {
echo 'This is not a symbolic link';
}
?>
Output:
This is not a symbolic link.
Explanation:
We create a symbolic link “demo” of the file “test.txt”. But, we used the physical file path as the parameter, not the symbolic link. That’s why the is_link() function returns FALSE and prints “This is not a symbolic link”.
Caution:
PHP cache the result of the is_link() function. So if you call this function multiple times in a script PHP sends you the cached result. The problem is if existence of the directory, you won’t get the up-to-date result. To get the latest result, clear the cache with clearstatcache() function. Check the code below-
<?php
$target = "test.txt";
$link = "demo";
symlink($target, $link);
var_dump(is_file($link));
// Some codes.
var_dump(is_file($link));
// Some codes.
clearstatcache();
var_dump(is_file($link));
// Some codes.
?>
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP is_link() Function
is_link() function is very useful to get know whether a filename is a symbolic link or not which is built-in PHP filesystem function.