PHP unlink symlink

user1323512 picture user1323512 · Jul 30, 2012 · Viewed 12.1k times · Source

when I create under windows a symlink (didn't test it yet under linux) and want to delete/unlink it again (I tried it with the unlink() - function), it always delete the symlink + original file. But I just want to delete the symlink. Isn't there any function for it?

Answer

Vernard Luz picture Vernard Luz · Aug 5, 2014

Check this answer: https://stackoverflow.com/a/12288764/3910083

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)
}