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

Most Popular Reviews

Latest News Articles


PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?