You are here

public function FileSystem::tempnam in Drupal 9

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

Creates a file with a unique filename in the specified directory.

PHP's tempnam() does not return a URI like we want. This function will return a URI if given a URI, or it will return a filepath if given a filepath.

Compatibility: normal paths and stream wrappers.

Parameters

string $directory: The directory where the temporary filename will be created.

string $prefix: The prefix of the generated temporary filename. Note: Windows uses only the first three characters of prefix.

Return value

string|bool The new temporary filename, or FALSE on failure.

Overrides FileSystemInterface::tempnam

See also

tempnam()

https://www.drupal.org/node/515192

1 call to FileSystem::tempnam()
FileSystem::saveData in core/lib/Drupal/Core/File/FileSystem.php
Saves a file to the specified destination without invoking file API.

File

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

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function tempnam($directory, $prefix) {
  $scheme = StreamWrapperManager::getScheme($directory);
  if ($this->streamWrapperManager
    ->isValidScheme($scheme)) {
    $wrapper = $this->streamWrapperManager
      ->getViaScheme($scheme);
    if ($filename = tempnam($wrapper
      ->getDirectoryPath(), $prefix)) {
      return $scheme . '://' . static::basename($filename);
    }
    else {
      return FALSE;
    }
  }
  else {

    // Handle as a normal tempnam() call.
    return tempnam($directory, $prefix);
  }
}