Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Working with Links

Unix links come in two types: hard links , which are files, and symlinks (also known as soft links), which are pointers to other files. The difference is crucial: if you delete a hard link, you delete the file (unless there are other hard links pointing to the same file), whereas if you delete a symlink, the original file remains untouched.

You can create hard links and symlinks in PHP using the link() and symlink() functions, both of which take a target and a link name as their only two parameters and return true if they were successful or false otherwise. For example:

    $result = link("/home/paul/myfile.txt", "/home/andrew/myfile.txt");
    if (!$result) {
            echo "Hard link could not be created!\n";
    } else {
            $result = symlink("/home/paul/myfile.txt", "/home/andrew/myfile.txt");
            if (!$result) {
                    echo "Symlink could not be created either!\n";
            }
    }

PHP also gives you the readlink() function that takes a link name as its only parameter and returns the target that the link points to. For example:

    $target = readlink("/home/andrew/myfile.txt");
    print $target;
    // prints /home/paul/myfile.txt
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page