Symbolic links using PHP

A symbol link is a type file widely supported by UNIX-like operating systems, including Linux. It allows users to create a file or directory which links to another directory. Consider the following on a Linux system:

$ echo "test" &lr; test
$ ln -s test newtest
$ cat newtest
test

The file 'newtest' is a symbolic link to the file `test'. Programs which read and write to `newtest' are redirecting and actually read and write to `test'.

Python vs. PHP: Choosing your next project's language

One can also create a symbolic link to a directory (or any other type of valid file system object).

Symbolic links allow users to greatly simplify a complex file system layout. Popular uses include consolidation of configuration files, maintaining file system layouts for backwards compatibility, and simplification of file paths.

Symbolic links on Windows systems

Windows does not support symbolic links to the extent of UNIX-like systems. Windows 2000 does allow users to create a link from one directory to another. See the following URL for more information http://support.microsoft.com/default.aspx?scid=kb;EN-US;q205524.

Shortcuts, as implemented in other versions of Windows, are not recognised as symbolic links by PHP and will be treated as standard files.

Using symbolic links with PHP

Creating symbolic links and finding the actual target of the link can be easily accomplished with PHP. Consider the following:

01 <?
02 if(strlen($PHP_SELF)) {
03 $file = $PHP_SELF;
04 $linkfile = $file."2";
05 } else {
06 $file = $argv[0];
07 $linkfile = $file."2";
08 }
09 if(file_exists($linkfile)) {
10 if(is_link($linkfile)) {
11 unlink($linkfile);
12 } else {
13 exit("$linkfile exists, but is not a symbolic link\n");
14 }
15 }
16 $ret = symlink($file,$linkfile);
17 if(!$ret) {
18 exit("Could not create symbolic link $linkfile\n");
19 }
20 if(is_link($linkfile)) {
21 $target = readlink($linkfile);
22 $real = realpath($linkfile);
23 } else {
24 $target = $real = "";
25 }
26 echo "Target = $target\nReal path = $real\n";
27 ?>

On line 02 of the script, we test to see if the variable $PHP_SELF has been set to the name of the script. This variable is set when the script is run by a Web server. If the variable is set, we save the script's file name in $file and create the name of the symbolic link as the script name with a `2' appended to it. If $PHP_SELF has no length, the script is being run from the command line. As such, the script name is defined as the first member of the variable $argv. We use this to construct $linkfile.

On line 09 we test if $linkfile already exists - the script will not be able to create the symbolic link if it already exists. Importantly, if the file does exist, line 10 tests if it is a symbolic link: it is reasonable that a file exists and is named $linkfile, but if it isn't a symbolic link we may delete a legitimate file!

To test if a file is a symbolic link, we use is_link(), which returns true if the file passed as its argument is a symbolic link. If the file is a symbolic link, we delete it using unlink(); otherwise, we exit, notifying the user. On line 16, we create the symbolic link $linkfile, pointing to $file, using symlink(). We test the result of this operation and, if it is false, we exit, notifying the user.

On line 20, we again test if the link exists. If so, we determine the target of the symbolic link $linkfile using readlink(). The result should be $file - that is, the script name. We also call realpath(), which expands all symbolic links in the path to $linkfile. We then output both values to the user, on line 26.

Readers looking to refine their skills should attempt to modify this script. Although we test on line 09 if the symbolic link already exists and on line 10 test to see if it is actually a symbolic link, we do not test to see if the symbolic link points to $file. A well-written script should always attempt to test for every condition.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about Hewlett-Packard AustraliaHPLinux

Show Comments
[]