W3cubDocs

/Drupal 8

protected function FileStorage::createDirectory

protected FileStorage::createDirectory($directory, $mode = 0777, $is_backwards_recursive = FALSE)

Ensures the requested directory exists and has the right permissions.

For compatibility with open_basedir, the requested directory is created using a recursion logic that is based on the relative directory path/tree: It works from the end of the path recursively back towards the root directory, until an existing parent directory is found. From there, the subdirectories are created.

Parameters

string $directory: The directory path.

int $mode: The mode, permissions, the directory should have.

bool $is_backwards_recursive: Internal use only.

Return value

bool TRUE if the directory exists or has been created, FALSE otherwise.

File

core/lib/Drupal/Component/PhpStorage/FileStorage.php, line 155

Class

FileStorage
Stores the code as regular PHP files.

Namespace

Drupal\Component\PhpStorage

Code

protected function createDirectory($directory, $mode = 0777, $is_backwards_recursive = FALSE) {
  // If the directory exists already, there's nothing to do.
  if (is_dir($directory)) {
    return TRUE;
  }
  // 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);
    }
    return $is_backwards_recursive ? TRUE : $status;
  }
  // If the parent directory and the requested directory does not exist and
  // could not be created above, walk the requested directory path back up
  // until an existing directory is hit, and from there, recursively create
  // the sub-directories. Only if that recursion succeeds, create the final,
  // originally requested subdirectory.
  return $this->createDirectory($parent, $mode, TRUE) && mkdir($directory) && chmod($directory, $mode);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!PhpStorage!FileStorage.php/function/FileStorage::createDirectory/8.1.x