function drupal_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

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

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

Return value

The new temporary filename, or FALSE on failure.

See also

tempnam()

http://drupal.org/node/515192

Related topics

4 calls to drupal_tempnam()
file_unmanaged_save_data in drupal/core/includes/file.inc
Saves a file to the specified destination without invoking file API.
GDToolkit::save in drupal/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
Implements \Drupal\system\Plugin\ImageToolkitInterface::save().
Tasks::connect in drupal/core/lib/Drupal/Core/Database/Driver/sqlite/Install/Tasks.php
Check database connection and attempt to create database if the database is missing.
update_manager_local_transfers_allowed in drupal/core/modules/update/update.manager.inc
Determines if file transfers will be performed locally.

File

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

Code

function drupal_tempnam($directory, $prefix) {
  $scheme = file_uri_scheme($directory);
  if (file_stream_wrapper_valid_scheme($scheme)) {
    $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
    if ($filename = tempnam($wrapper
      ->getDirectoryPath(), $prefix)) {
      return $scheme . '://' . drupal_basename($filename);
    }
    else {
      return FALSE;
    }
  }
  else {

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