savinger
savinger

Reputation: 6714

Deleting Symbolic Links in PHP

What is the proper way to delete symbolic links, preserving what they link to? What is the proper way to delete what they link to? Which would unlink do? There seems to be some ambiguity.

Through a little testing, symbolic links respond to is_file and is_dir according to what they point to, as well as returning true to is_link.

Upvotes: 5

Views: 6856

Answers (1)

user557846
user557846

Reputation:

unlink() is the correct approach

code snippet from a project of mine, to only delete if it was a symlink

if(file_exists($linkfile)) {
    if(is_link($linkfile)) {
        unlink($linkfile);
    } else {
        exit("$linkfile exists but not symbolic link\n");
    }
}

readlink(), returns the target of a link, you can run unlink on that

if(is_link($linkfile)) {
      $target = readlink($linkfile)
      unlink($target)
}

Upvotes: 15

Related Questions