- // Otherwise, try to create the directory and ensure to set its permissions,
- // because mkdir() obeys the umask of the current process.
- if (is_dir($parent = dirname($directory))) {
- // If the parent directory exists, then the backwards recursion must end,
- // regardless of whether the subdirectory could be created.
- if ($status = mkdir($directory)) {
- // Only try to chmod() if the subdirectory could be created.
- $status = chmod($directory, $mode);
+
+ // If the parent directory doesn't exist, try to create it.
+ $parent_exists = is_dir($parent = dirname($directory));
+ if (!$parent_exists) {
+ $parent_exists = $this->createDirectory($parent, $mode);
+ }
+
+ // If parent exists, try to create the directory and ensure to set its
+ // permissions, because mkdir() obeys the umask of the current process.
+ if ($parent_exists) {
+ // We hide warnings and ignore the return because there may have been a
+ // race getting here and the directory could already exist.
+ @mkdir($directory);
+ // Only try to chmod() if the subdirectory could be created.
+ if (is_dir($directory)) {
+ // Avoid writing permissions if possible.
+ if (fileperms($directory) !== $mode) {
+ return chmod($directory, $mode);
+ }
+ return TRUE;
+ }
+ else {
+ // Something failed and the directory doesn't exist.
+ trigger_error('mkdir(): Permission Denied', E_USER_WARNING);