function drupal_tempnam

Same name and namespace in other branches
  1. 8.9.x core/includes/ \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.


$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


Related topics

6 calls to drupal_tempnam()
file_unmanaged_save_data in includes/
Saves a string to the specified destination without invoking file API.
image_gd_save in modules/system/
GD helper to write an image resource to a destination file.
LocaleExportFunctionalTest::testExportTranslation in modules/locale/locale.test
Test exportation of translations.
LocaleImportFunctionalTest::importPoFile in modules/locale/locale.test
Helper function: import a standalone .po file in a given language.
LocalePluralFormatTest::importPoFile in modules/locale/locale.test
Imports a standalone .po file in a given language.

... See full list


includes/, line 2649


function drupal_tempnam($directory, $prefix) {
    $scheme = file_uri_scheme($directory);
    if ($scheme && 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);

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.