|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52176 unlink() cannot delete symbolic link which points to a directory
Submitted: 2010-06-24 19:37 UTC Modified: 2011-12-15 13:54 UTC
Avg. Score:4.2 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:2 (40.0%)
From: o_shes01 at uni-muenster dot de Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.3.2 OS: Windows Vista SP2
Private report: No CVE-ID: None
 [2010-06-24 19:37 UTC] o_shes01 at uni-muenster dot de
unlink($fname) fails if the file named by $fname is a symbolic link which points to a directory. 

Such a link (not the directory which it points to!) can however be deleted via "rmdir($fname)", which is also a bug (IMHO). 

Note: if the link points to a regular file (not directory), then unlink works as expected (deletes the link, not the file pointed to). 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-15 15:55 UTC]
-Status: Open +Status: Bogus
 [2011-11-15 15:55 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

 [2011-12-15 13:54 UTC] o_shes01 at uni-muenster dot de
Assuming that $fname is absolute filename of a symbolic link, which points to a directory. 

On Linux, unlink($fname) deletes the link itself, and rmdir($fname) does NOT work. 

On Windows, unlink($fname) does not work, and rmdir($fname) deletes the link. 

This behaviour is not documented in any way. 

If this is not a bug, then which is the correct way to delete a symbolic link which points to a directory?
 [2013-07-03 15:39 UTC] kulbakin at ya dot ru
Have similar issue with Windows 7 64 bit php 5.3.26
unlink() for a link created earlier with symlink() call for a directory returns

Warning: unlink([...]): Permission denied  in [...] on line [...]

and rmdir() works for it, but on *nix unlink() works for such links.
 [2013-10-21 19:38 UTC] lucas dot ekrause at gmail dot com

> Please double-check the documentation [...]

I did, but didn't find anything documenting this issue.
Can you please post a link to the page which is documenting it?
 [2017-07-25 14:44 UTC] DiablosOffens at gmx dot de
There is actually a difference between Linux and Windows here.
This difference is not documented in any way, not by PHP nor by Microsoft.

Sure, the documentation for DeleteFile has a section "Symbolic link behavior", but it doesn't mention the special case for a directory, because the whole site ( only mentions the case where a file path is passed to this function. So it holds true that unlink works for symbolic links to files, but at least until Windows Server 2012 R2 it is proven that it doesn't work for symbolic links to directories.

It would be nice to see a bug fix for PHP, to get less surprises on different platforms.

Note: The C-runtime function "unlink" from Microsoft's msvcr*.dll is only a wrapper for the Win32-API function "DeleteFile".
 [2017-07-25 15:51 UTC] DiablosOffens at gmx dot de
I have to correct me.
Microsoft documents it at this page:

But this doesn't fix the problem at all. It only proves that unlink behaves different on Windows than the POSIX standard suggests.
 [2022-07-28 16:19 UTC] adrien dot foulon at tukan dot hu
God, 2022 and php8 still has this exact issue

Behavior is different between windows and linux
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 14 20:01:31 2024 UTC