You are here

public function FileSystem::getTempDirectory in Drupal 9

Same name and namespace in other branches
  1. 8 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 629

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