php rmdir() permission denied problem in Windows
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
}
closedir($handle);
rmdir("dirname");
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
Comments(9)
it solved my problem
thank you very much!!