Search code examples
phppermissionsmkdir

distinguish what caused mkdir error


PHP's mkdir returns false on failure. Failure can be:

  • Folder already exists
  • Some other error, permissions error perhaps?

Is there a way to distinguish whether the failure occurred for one reason and not the other? I'd like my script to be able to continue operation if mkdir returns false as long as the folder already exists, but handle it otherwise if it was some other error.


Solution

  • Sure, just check if the directory exists using is_dir() once mkdir() fails.

    This will tell you if the directory exists, however it may not be conclusive, as permissions might also prevent you from checking this (I believe the parent directories need the +x permission in order to traverse into subdirectories).