Remove a symlink with PHP on Windows
Asked Answered
B

1

5

How do I remove a symlink with PHP on windows?

Running this:

mkdir('test');
symlink('test', 'test2');
unlink('test2');

Gives the following error:

PHP Warning:  unlink(test2): Permission denied in C:\path\to\app\testlink.php on line 4
PHP Stack trace:
PHP   1. {main}() C:\path\to\app\testlink.php:0
PHP   2. unlink() C:\path\to\app\testlink.php:4

The directory and symlink were made correctly, just not removed.

Running:

  • PHP 5.4.9 (CLI)
  • Windows 8
Birchfield answered 15/8, 2013 at 21:55 Comment(3)
Edit: Missed the "Windows" bit. Actually according to the docs, Windows DOES support it, so it should work?Referential
The docs have a number of Windows examples, I'd give the page a read; php.net/manual/en/function.symlink.phpReferential
@Referential like I said it does create the symlink correctly, its the unlink function thats not working.Birchfield
B
7

Ok, I figured it out. So Ill leave this here for future reference:

To remove a symlink to a directory use the rmdir function:

mkdir('test');
symlink('test', 'test2');
rmdir('test2');

unlink is for removing files.

Birchfield answered 15/8, 2013 at 22:16 Comment(3)
This only works for Windows. On *nix you still have to use unlink for symlinks, no matter what they point to.Zworykin
@Zworykin notice the question explicitly says this is a Windows issue.Birchfield
sure but I thought it was worth pointing out that you have to do platform sniffing if you want your code to work cross platform. That wasn't obvious to me and I had to do some tests to find out.Zworykin

© 2022 - 2024 — McMap. All rights reserved.