You are here

public function FileSystem::prepareDirectory in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::prepareDirectory()

Checks that the directory exists and is writable.

Directories need to have execute permissions to be considered a directory by FTP servers, etc.

Parameters

string $directory: A string reference containing the name of a directory path or URI. A trailing slash will be trimmed from a path.

int $options: A bitmask to indicate if the directory should be created if it does not exist (FileSystemInterface::CREATE_DIRECTORY) or made writable if it is read-only (FileSystemInterface::MODIFY_PERMISSIONS).

Return value

bool TRUE if the directory exists (or was created) and is writable. FALSE otherwise.

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 519

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;
}