function drupal_mkdir

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.

Parameters

$uri: A URI or pathname.

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

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

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

Return value

Boolean TRUE on success, or FALSE on failure.

See also

mkdir()

http://drupal.org/node/515192

Related topics

11 calls to drupal_mkdir()
ArchiveTar::_dirCheck in drupal/core/lib/Drupal/Component/Archiver/ArchiveTar.php
Check if a directory exists and create it (including parent dirs) if not.
ArchiveTar::_extractList in drupal/core/lib/Drupal/Component/Archiver/ArchiveTar.php
Connection::createDatabase in drupal/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Overrides \Drupal\Core\Database\Connection::createDatabase().
DirectoryTest::testFileCheckLocalDirectoryHandling in drupal/core/modules/system/lib/Drupal/system/Tests/File/DirectoryTest.php
Test local directory handling functions.
drupal_install_mkdir in drupal/core/includes/install.inc
Creates a directory with the specified permissions.

... See full list

File

drupal/core/includes/file.inc, line 1735
API for handling file uploads and server file management.

Code

function drupal_mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
  if (!isset($mode)) {

    // Configuration system stores default mode as strings.
    $mode = FALSE;

    // During early update there's no container.
    if (is_object(Drupal::getContainer())) {
      $mode = octdec(config('system.file')
        ->get('chmod.directory'));
    }
    if (!$mode) {
      $mode = 0775;
    }
  }

  // If the URI has a scheme, don't override the umask - schemes can handle this
  // issue in their own implementation.
  if (file_uri_scheme($uri)) {
    return _drupal_mkdir_call($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);
    array_pop($components);
    $recursive_path = '';
    foreach ($components as $component) {
      $recursive_path .= $component;
      if (!file_exists($recursive_path)) {
        if (!_drupal_mkdir_call($recursive_path, $mode, FALSE, $context)) {
          return FALSE;
        }

        // Not necessary to use drupal_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 (!_drupal_mkdir_call($uri, $mode, FALSE, $context)) {
    return FALSE;
  }

  // Not necessary to use drupal_chmod() as there is no scheme.
  return chmod($uri, $mode);
}