Removing symbolic links in PHP

What is the correct way to remove symbolic links while preserving what they link to? What is the correct way to remove links? What would unlink do? There seems to be some ambiguity .

After a little testing, symbolic links respond to is_file and is_dir according to what they point to, and also return it trueto is_link .

+5
source share
1 answer

unlink() - the right approach

code snippet from my project to remove it only if it's a symbolic link

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

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

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

All Articles