php rmdir() permission denied problem in Windows

Saturday 21 Feb 2009 11:26

For anyone getting that annoying 'permission denied' error when using PHP's rmdir() function in Windows I finally found a solution. I'd always passed this problem off as something to do with NTFS and Windows sharing permissions and pretty much ignored it as it works online in Linux. A common use for rmdir() is in a loop, reading files and directories and deleting them. If you attempt to delete the directory you are reading within, without closing it first you will get this error, so always call closedir($handle) first, which embarrassingly enough was something I wasn't doing (tut tut).

$handle = opendir("dirname");
while(($file = readdir($handle)) !== false)
// unlink files, rmdir directories

I know there are other instances where the 'permission denied' error occurs but in this particular case I was able to solve my problem with the above code. Note I read on one forum the solution is to place an @ in front of rmdir(), ie. @rmdir(). This is error surpression and doesn't solve anything except make PHP NOT print the error!

It seems LINUX does things differently, removing the 'in-use' directory after the current PHP script process has finished, hence why the error does not occur in LINUX.

Related articles


20 Oct 2011 02:27 by Neng
I try to solve this problem about 2 days. Thank you so much.
8 Oct 2010 08:11 by phpnewbie
WOW thank you! I've been searching for the solution for about 2 hours and nothing helped - until... :) I used the @ in front of rmdir. Thanks!
21 Sep 2010 02:28 by danibr
thank you!
it solved my problem
30 Jun 2010 10:43 by Cornelius
Thanks for the help- I was pulling my hair out trying to figure this out, and was thinking that it was a Windoze issue until I came across your post.
1 May 2010 06:42 by jonas
nice solution, thank for help, i advise you try to add chmod($handle,0777);
25 Feb 2010 08:02 by Oleg
Thanx, you helped me a lot. I had hard times with this problem, now it's gone. Good luck!
23 Oct 2009 11:32 by I am

thank you very much!!
11 Aug 2009 11:21 by BootBlock
This site has less updates than mine! Update it, ya winkle!
8 May 2009 11:54 by marcinrey
Thank you! This solved my problem.