You are here

public function FileSystem::getTempDirectory in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::getTempDirectory()
  2. 10 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::getTempDirectory()

Gets the path of the configured temporary directory.

If the path is not set, it will fall back to the OS-specific default if set, otherwise a directory under the public files directory. It will then set this as the configured directory.

Return value

string A string containing the path to the temporary directory.

Overrides FileSystemInterface::getTempDirectory

File

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

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function getTempDirectory() {

  // Use settings.
  $temporary_directory = $this->settings
    ->get('file_temp_path');
  if (!empty($temporary_directory)) {
    return $temporary_directory;
  }

  // Fallback to config for Backwards compatibility.
  // This service is lazy-loaded and not injected, as the file_system service
  // is used in the install phase before config_factory service exists. It
  // will be removed before Drupal 9.0.0.
  if (\Drupal::hasContainer()) {
    $temporary_directory = \Drupal::config('system.file')
      ->get('path.temporary');
    if (!empty($temporary_directory)) {
      @trigger_error("The 'system.file' config 'path.temporary' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Set 'file_temp_path' in settings.php instead. See https://www.drupal.org/node/3039255", E_USER_DEPRECATED);
      return $temporary_directory;
    }
  }

  // Fallback to OS default.
  $temporary_directory = FileSystemComponent::getOsTemporaryDirectory();
  if (empty($temporary_directory)) {

    // If no directory has been found default to 'files/tmp'.
    $temporary_directory = PublicStream::basePath() . '/tmp';

    // Windows accepts paths with either slash (/) or backslash (\), but
    // will not accept a path which contains both a slash and a backslash.
    // Since the 'file_public_path' variable may have either format, we
    // sanitize everything to use slash which is supported on all platforms.
    $temporary_directory = str_replace('\\', '/', $temporary_directory);
  }
  return $temporary_directory;
}