function FileSystem::prepareDirectory

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

Overrides FileSystemInterface::prepareDirectory

1 call to FileSystem::prepareDirectory()
FileSystem::prepareDestination in core/lib/Drupal/Core/File/FileSystem.php
Prepares the destination for a file copy or move operation.

File

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

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function prepareDirectory(&$directory, $options = self::MODIFY_PERMISSIONS) {
    if (!$this->streamWrapperManager
        ->isValidUri($directory)) {
        // Only trim if we're not dealing with a stream.
        $directory = rtrim($directory, '/\\');
    }
    if (!is_dir($directory)) {
        if (!($options & static::CREATE_DIRECTORY)) {
            return FALSE;
        }
        // Let mkdir() recursively create directories and use the default
        // directory permissions.
        $success = @$this->mkdir($directory, NULL, TRUE);
        if ($success) {
            return TRUE;
        }
        // If the operation failed, check again if the directory was created
        // by another process/server, only report a failure if not. In this case
        // we still need to ensure the directory is writable.
        if (!is_dir($directory)) {
            return FALSE;
        }
    }
    $writable = is_writable($directory);
    if (!$writable && $options & static::MODIFY_PERMISSIONS) {
        return $this->chmod($directory);
    }
    return $writable;
}

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