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

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;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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

Crucial Ballistix Elite 32GB Kit (4 x 8GB) DDR4-3000 UDIMM

Learn more >

Gadgets & Things

Lexar® Professional 1000x microSDHC™/microSDXC™ UHS-II cards

Learn more >

Family Friendly

Lexar® JumpDrive® S57 USB 3.0 flash drive 

Learn more >

Stocking Stuffer

Plox Star Wars Death Star Levitating Bluetooth Speaker

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Latest News Articles


GGG Evaluation Team

Kathy Cassidy


First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni


For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell


The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi


The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott


My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?