PHP unlink permission denied error on Windows

December 22nd, 2010 | Tags: ,

If you keep getting permission denied errors on windows when using PHP’s unlink function, and you already checked permissions, it’s probably the read-only flag on the file. I don’t know why PHP does this, but it won’t delete a file marked read-only. Thanks to a Daniel V for pointing this out and saving me a lot of time.

@chmod($dir, 0777) seems to clear the read-only flag, as pointed out by hodgman at ali dot com dot au PHP’s chmod docs.

Also, make sure the file isn’t still open for reading, as that will also prevent unlike from doing its job. It’s somewhat common to want to read a file and then delete it, and sometimes easy to forget to close it before doing so.

One Response to “PHP unlink permission denied error on Windows”

  1. Michael says:

    I had this problem when trying to compress a directory and send it. Aftering sending, unlink on the .tar file would alwways fail, though I had fclose’d the file. I fixed it by unset’ing the object used to compress the file, i.e. unset($phar);

Leave a Reply