public function FileSystem::mkdir

public FileSystem::mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL)

Creates a directory, optionally creating missing components in the path to the directory.

When PHP's mkdir() creates a directory, the requested mode is affected by the process's umask. This function overrides the umask and sets the mode explicitly for all directory components created.

@todo Update with open_basedir compatible recursion logic from \Drupal\Component\PhpStorage\FileStorage::ensureDirectory().

Parameters

string $uri: A URI or pathname.

int $mode: Mode given to created directories. Defaults to the directory mode configured in the Drupal installation. It must have a leading zero.

bool $recursive: Create directories recursively, defaults to FALSE. Cannot work with a mode which denies writing or execution to the owner of the process.

resource $context: Refer to http://php.net/manual/ref.stream.php

Return value

bool Boolean TRUE on success, or FALSE on failure.

Overrides FileSystemInterface::mkdir

See also

mkdir()

https://www.drupal.org/node/515192

File

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

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 ($this->uriScheme($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.
    $uri = str_replace('/', DIRECTORY_SEPARATOR, $uri);
    // 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);
}

© 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!Core!File!FileSystem.php/function/FileSystem::mkdir/8.1.x