function drush_find_tmp

8.0.x drush_find_tmp()
6.x drush_find_tmp()
7.x drush_find_tmp()
4.x drush_find_tmp()
5.x drush_find_tmp()
master drush_find_tmp()

Returns the path to a temporary directory.

This is a custom version of Drupal's file_directory_path(). We can't directly rely on sys_get_temp_dir() as this path is not valid in some setups for Mac, and we want to honor an environment variable (used by tests).

Related topics

6 calls to drush_find_tmp()
drush_directory_cache in includes/
The path to the global cache directory.
drush_tempdir in includes/
Creates a temporary directory and return its path.
drush_tempnam in includes/
make_tmp in commands/make/
Find, and possibly create, a temporary directory.
SqlBase::dumpFile in lib/Drush/Sql/SqlBase.php

... See full list


includes/, line 426
Filesystem utilities.


function drush_find_tmp() {
  static $temporary_directory;

  if (!isset($temporary_directory)) {
    $directories = array();

    // Get user specific and operating system temp folders from system environment variables.
    // See
    $tempdir = getenv('TEMP');
    if (!empty($tempdir)) {
      $directories[] = $tempdir;
    $tmpdir = getenv('TMP');
    if (!empty($tmpdir)) {
      $directories[] = $tmpdir;
    // Operating system specific dirs.
    if (drush_is_windows()) {
      $windir = getenv('WINDIR');
      if (isset($windir)) {
        // WINDIR itself is not writable, but it always contains a /Temp dir,
        // which is the system-wide temporary directory on older versions. Newer
        // versions only allow system processes to use it.
        $directories[] = $windir . '/Temp';
    else {
      $directories[] = '/tmp';
    $directories[] = sys_get_temp_dir();

    foreach ($directories as $directory) {
      if (is_dir($directory) && is_writable($directory)) {
        $temporary_directory = $directory;

    if (empty($temporary_directory)) {
      // If no directory has been found, create one in cwd.
      $temporary_directory = drush_cwd() . '/tmp';
      drush_mkdir($temporary_directory, TRUE);
      if (!is_dir($temporary_directory)) {
        return drush_set_error('DRUSH_UNABLE_TO_CREATE_TMP_DIR', dt("Unable to create a temporary directory."));

  return $temporary_directory;