W3cubDocs

/Drupal 8

function file_directory_temp

file_directory_temp()

Gets and sets the path of the configured temporary directory.

Return value

mixed|null A string containing the path to the temporary directory.

Related topics

File interface
Common file handling functions.

File

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

Code

function file_directory_temp() {
  $temporary_directory = \Drupal::config('system.file')->get('path.temporary');
  if (empty($temporary_directory)) {
    // Needs set up.
    $config = \Drupal::configFactory()->getEditable('system.file');
    $temporary_directory = file_directory_os_temp();

    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);
    }
    // Save the path of the discovered directory. Do not check config schema on
    // save.
    $config->set('path.temporary', (string) $temporary_directory)->save(TRUE);
  }

  return $temporary_directory;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!includes!file.inc/function/file_directory_temp/8.1.x