function FileStorage::createDirectory

Same name in other branches
  1. 9 core/lib/Drupal/Component/PhpStorage/FileStorage.php \Drupal\Component\PhpStorage\FileStorage::createDirectory()
  2. 8.9.x core/lib/Drupal/Component/PhpStorage/FileStorage.php \Drupal\Component\PhpStorage\FileStorage::createDirectory()
  3. 11.x core/lib/Drupal/Component/PhpStorage/FileStorage.php \Drupal\Component\PhpStorage\FileStorage::createDirectory()

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.

Return value

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

1 call to FileStorage::createDirectory()
FileStorage::ensureDirectory in core/lib/Drupal/Component/PhpStorage/FileStorage.php
Ensures the directory exists, has the right permissions, and a .htaccess.

File

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

Class

FileStorage
Stores the code as regular PHP files.

Namespace

Drupal\Component\PhpStorage

Code

protected function createDirectory($directory, $mode = 0777) {
    // If the directory exists already, there's nothing to do.
    if (is_dir($directory)) {
        return TRUE;
    }
    // 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);
        }
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.