In PHP, the scandir function can list all files and subdirectories within a specified directory. However, scandir only returns a simple list of files and directories without differentiating symbolic links. To list symbolic links and the target files they point to, we need to use scandir together with is_link and readlink.
The scandir function returns all files and subdirectories in a specified directory, sorted alphabetically by default. The basic usage is as follows:
<span><span><span class="hljs-variable">$files</span></span><span> = </span><span><span class="hljs-title function_ invoke__">scandir</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>);
</span></span>
The $directory parameter is the target directory to scan. The return value is an array containing all files and subdirectories under that directory. Note that the list returned by scandir includes the special entries . and .., which represent the current and parent directories.
To check whether a file or directory is a symbolic link, you can use PHP’s is_link function. It checks whether a given path is a symbolic link.
<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">is_link</span></span><span>(</span><span><span class="hljs-variable">$path</span></span><span>)) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<span class="hljs-subst">$path</span></span></span><span> is a symbolic link\n";
}
</span></span>
The readlink function can be used to obtain the actual file path that a symbolic link points to. If the given path is a symbolic link, readlink will return the target path; otherwise, it returns false.
<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">is_link</span></span><span>(</span><span><span class="hljs-variable">$path</span></span><span>)) {
</span><span><span class="hljs-variable">$target</span></span><span> = </span><span><span class="hljs-title function_ invoke__">readlink</span></span><span>(</span><span><span class="hljs-variable">$path</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<span class="hljs-subst">$path</span></span></span><span> points to </span><span><span class="hljs-subst">$target</span></span><span>\n";
}
</span></span>
Now we can combine the above code to list symbolic links in a specified directory along with their target files.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Specify the directory to scan</span></span><span>
</span><span><span class="hljs-variable">$directory</span></span><span> = </span><span><span class="hljs-string">'/path/to/directory'</span></span><span>;
<p></span>// Use scandir to get the directory list<br>
$files = scandir($directory);</p>
<p>// Iterate through the directory list<br>
foreach ($files as $file) {<br>
// Exclude . and .. entries<br>
if ($file === '.' || $file === '..') {<br>
continue;<br>
}</p>
</span><span><span class="hljs-variable">$filePath</span></span><span> = </span><span><span class="hljs-variable">$directory</span></span><span> . DIRECTORY_SEPARATOR . </span><span><span class="hljs-variable">$file</span></span><span>;
</span><span><span class="hljs-comment">// Check if the file is a symbolic link</span></span><span>
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">is_link</span></span><span>(</span><span><span class="hljs-variable">$filePath</span></span><span>)) {
</span><span><span class="hljs-comment">// Get the target file of the symbolic link</span></span><span>
</span><span><span class="hljs-variable">$target</span></span><span> = </span><span><span class="hljs-title function_ invoke__">readlink</span></span><span>(</span><span><span class="hljs-variable">$filePath</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<span class="hljs-subst">$file</span></span></span><span> is a symbolic link pointing to </span><span><span class="hljs-subst">$target</span></span><span>\n";
}
}
?>
scandir($directory): Gets all files and subdirectories in a directory.
is_link($filePath): Checks whether the current file is a symbolic link.
readlink($filePath): Retrieves the path that the symbolic link points to.
Suppose the directory /path/to/directory contains the following:
file1.txt (regular file)
symlink1 (symbolic link pointing to /path/to/target1.txt)
symlink2 (symbolic link pointing to /path/to/target2.txt)
After running the code above, the output will be:
<span><span>symlink1 is a symbolic link pointing to /</span><span><span class="hljs-type">path</span></span><span>/</span><span><span class="hljs-keyword">to</span></span><span>/target1.txt
symlink2 is a symbolic link pointing to /</span><span><span class="hljs-type">path</span></span><span>/</span><span><span class="hljs-keyword">to</span></span><span>/target2.txt
</span></span>
By combining scandir with is_link and readlink, we can easily list symbolic links in a specified directory along with their target files. This approach helps us better manage and analyze symbolic links in the file system, especially when dealing with complex directory structures.