function FileSystem::mkdir

Same name in other branches
  1. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::mkdir()
  2. 10 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::mkdir()
  3. 11.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::mkdir()

Overrides FileSystemInterface::mkdir

1 call to FileSystem::mkdir()
FileSystem::prepareDirectory in core/lib/Drupal/Core/File/FileSystem.php
Checks that the directory exists and is writable.

File

core/lib/Drupal/Core/File/FileSystem.php, line 180

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
    if (!isset($mode)) {
        $mode = $this->settings
            ->get('file_chmod_directory', static::CHMOD_DIRECTORY);
    }
    // If the URI has a scheme, don't override the umask - schemes can handle
    // this issue in their own implementation.
    if (StreamWrapperManager::getScheme($uri)) {
        return $this->mkdirCall($uri, $mode, $recursive, $context);
    }
    // If recursive, create each missing component of the parent directory
    // individually and set the mode explicitly to override the umask.
    if ($recursive) {
        // Ensure the path is using DIRECTORY_SEPARATOR, and trim off any trailing
        // slashes because they can throw off the loop when creating the parent
        // directories.
        $uri = rtrim(str_replace('/', DIRECTORY_SEPARATOR, $uri), DIRECTORY_SEPARATOR);
        // Determine the components of the path.
        $components = explode(DIRECTORY_SEPARATOR, $uri);
        // If the filepath is absolute the first component will be empty as there
        // will be nothing before the first slash.
        if ($components[0] == '') {
            $recursive_path = DIRECTORY_SEPARATOR;
            // Get rid of the empty first component.
            array_shift($components);
        }
        else {
            $recursive_path = '';
        }
        // Don't handle the top-level directory in this loop.
        array_pop($components);
        // Create each component if necessary.
        foreach ($components as $component) {
            $recursive_path .= $component;
            if (!file_exists($recursive_path)) {
                if (!$this->mkdirCall($recursive_path, $mode, FALSE, $context)) {
                    return FALSE;
                }
                // Not necessary to use self::chmod() as there is no scheme.
                if (!chmod($recursive_path, $mode)) {
                    return FALSE;
                }
            }
            $recursive_path .= DIRECTORY_SEPARATOR;
        }
    }
    // Do not check if the top-level directory already exists, as this condition
    // must cause this function to fail.
    if (!$this->mkdirCall($uri, $mode, FALSE, $context)) {
        return FALSE;
    }
    // Not necessary to use self::chmod() as there is no scheme.
    return chmod($uri, $mode);
}

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