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!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
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 Articles

Resources

PCW Evaluation Team

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?