PHP unlink symlink
Asked Answered
M

5

5

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?

Marcomarconi answered 30/7, 2012 at 7:57 Comment(0)
R
9

Check this answer: https://mcmap.net/q/1887330/-deleting-symbolic-links-in-php

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)
}
Rebarbative answered 5/8, 2014 at 11:17 Comment(0)
J
0
  1. Rename the symbolic link to something like "my_link" in order to reuse the same PHP file.
  2. Create the PHP file with the appropriate code. I used "unlink.php". Simple and easy to remember.
  3. Upload the PHP file to the directory containing the link.
  4. Type in the URL to the file.

The result is that the file disappears without a trace.

<?php
unlink ('my_link');
?>
Janessajanet answered 30/7, 2012 at 8:3 Comment(0)
A
0

Just make sure you are using soft link, not hard links

check the usage of Mklink from here

Ada answered 30/7, 2012 at 8:7 Comment(2)
i created the link with the symlink() - functionMarcomarconi
What windows system are you using XP/VISTA/7 or Server 2000/3/8?Ada
D
0

On linux, to be safe I would just go with

shell_exec('rm ' . $file);
Democritus answered 18/3, 2021 at 22:29 Comment(0)
B
-2

Read this:-

http://php.net/manual/en/function.symlink.php

try this:-

symlink($target, $link);
unlink($link);
unlink($target);
Busily answered 30/7, 2012 at 8:3 Comment(1)
i want to delete the symlink, why i should create a new link then?Marcomarconi

© 2022 - 2024 — McMap. All rights reserved.