Computerworld
Symbolic links using PHP
Gavin Sherry (PC World)  28 November, 2003 08:56

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'.

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.

Computerworld Buyer's Guide - Vendors Matched to this Article
More about Linux, Microsoft

Comments

Post new comment

Login or register to link comments to your user profile, or you may also post a comment without being logged in.
The content of this field is kept private and will not be shown publicly.
Add to Google
Computerworld Buyer's Guide - Vendors Matched to this Article
Zones
Zone logoZones provide focussed content from Computerworld and leading technology partners.
Newsletter Subscription
Newsletter Subscription
Sign up for our Computerworld newsletters!
Syndicate content
 

Computerworld Webinar

Thursday, June 11th, 2009
10:30am EST (Sydney, Australia)
Screening at your PC

Computerworld is hosting a 30 minute live webinar to help you to learn how unified communications can save you money, foster innovation and business agility by making it easier for people to find, reach and collaborate with one another.

Register Now

Computerworld Community Comments
Whitepaper

Best Practices in Lifecycle Management

This white paper compares solutions from KACE, Altiris, LANDesk, and Microsoft. Read on for best practices, functional solution comparisons and cost comparisons. Determine overall value easily and quickly.

Enterprise IT Buyer's Guide
Find Technology Vendors Fast
 
Find vendors by name | Find by category
Sponsored Links
 
Send Us E-mail | Privacy Policy
Features List | Media Kit | Advertising | Contact Us

Copyright 2009 IDG Communications. ABN 14 001 592 650. All rights reserved.
Reproduction in whole or in part in any form or medium without express written permission of IDG Communications is prohibited.