Deleting Symbolic Links in PHP
Asked Answered
P

1

5

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.

Premise answered 5/9, 2012 at 19:46 Comment(0)
B
15

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)
}
Bus answered 5/9, 2012 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.