wp_tempnam

The timeline below displays how wordpress function wp_tempnam has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Returns a filename of a temporary unique file.
 *
 * Please note that the calling function must delete or move the file.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable
 * temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string A writable filename.
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || in_array($filename, array('.', '/', '\\'), true)) {
        $filename = uniqid();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory.
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use its parent directory name instead.
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    // Suffix some random data to avoid filename conflicts.
    $temp_filename .= '-' . wp_generate_password(6, false);
    $temp_filename .= '.tmp';
    $temp_filename = wp_unique_filename($dir, $temp_filename);
    /*
     * Filesystems typically have a limit of 255 characters for a filename.
     *
     * If the generated unique filename exceeds this, truncate the initial
     * filename and try again.
     *
     * As it's possible that the truncated filename may exist, producing a
     * suffix of "-1" or "-10" which could exceed the limit again, truncate
     * it to 252 instead.
     */
    $characters_over_limit = strlen($temp_filename) - 252;
    if ($characters_over_limit > 0) {
        $filename = substr($filename, 0, -$characters_over_limit);
        return wp_tempnam($filename, $dir);
    }
    $temp_filename = $dir . $temp_filename;
    $fp = @fopen($temp_filename, 'x');
    if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
        return wp_tempnam($filename, $dir);
    }
    if ($fp) {
        fclose($fp);
    }
    return $temp_filename;
}

WordPress Version: 6.3

/**
 * Returns a filename of a temporary unique file.
 *
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable
 * temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string A writable filename.
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || in_array($filename, array('.', '/', '\\'), true)) {
        $filename = uniqid();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory.
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use its parent directory name instead.
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    // Suffix some random data to avoid filename conflicts.
    $temp_filename .= '-' . wp_generate_password(6, false);
    $temp_filename .= '.tmp';
    $temp_filename = wp_unique_filename($dir, $temp_filename);
    /*
     * Filesystems typically have a limit of 255 characters for a filename.
     *
     * If the generated unique filename exceeds this, truncate the initial
     * filename and try again.
     *
     * As it's possible that the truncated filename may exist, producing a
     * suffix of "-1" or "-10" which could exceed the limit again, truncate
     * it to 252 instead.
     */
    $characters_over_limit = strlen($temp_filename) - 252;
    if ($characters_over_limit > 0) {
        $filename = substr($filename, 0, -$characters_over_limit);
        return wp_tempnam($filename, $dir);
    }
    $temp_filename = $dir . $temp_filename;
    $fp = @fopen($temp_filename, 'x');
    if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
        return wp_tempnam($filename, $dir);
    }
    if ($fp) {
        fclose($fp);
    }
    return $temp_filename;
}

WordPress Version: 5.5

/**
 * Returns a filename of a temporary unique file.
 *
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable
 * temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string A writable filename.
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || in_array($filename, array('.', '/', '\\'), true)) {
        $filename = uniqid();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory.
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use its parent directory name instead.
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    // Suffix some random data to avoid filename conflicts.
    $temp_filename .= '-' . wp_generate_password(6, false);
    $temp_filename .= '.tmp';
    $temp_filename = $dir . wp_unique_filename($dir, $temp_filename);
    $fp = @fopen($temp_filename, 'x');
    if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
        return wp_tempnam($filename, $dir);
    }
    if ($fp) {
        fclose($fp);
    }
    return $temp_filename;
}

WordPress Version: 5.4

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || '.' == $filename || '/' == $filename || '\\' == $filename) {
        $filename = uniqid();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory.
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use its parent directory name instead.
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    // Suffix some random data to avoid filename conflicts.
    $temp_filename .= '-' . wp_generate_password(6, false);
    $temp_filename .= '.tmp';
    $temp_filename = $dir . wp_unique_filename($dir, $temp_filename);
    $fp = @fopen($temp_filename, 'x');
    if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
        return wp_tempnam($filename, $dir);
    }
    if ($fp) {
        fclose($fp);
    }
    return $temp_filename;
}

WordPress Version: 5.1

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || '.' == $filename || '/' == $filename || '\\' == $filename) {
        $filename = uniqid();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use its parent directory name instead.
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    // Suffix some random data to avoid filename conflicts
    $temp_filename .= '-' . wp_generate_password(6, false);
    $temp_filename .= '.tmp';
    $temp_filename = $dir . wp_unique_filename($dir, $temp_filename);
    $fp = @fopen($temp_filename, 'x');
    if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
        return wp_tempnam($filename, $dir);
    }
    if ($fp) {
        fclose($fp);
    }
    return $temp_filename;
}

WordPress Version: 4.6

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || '.' == $filename || '/' == $filename || '\\' == $filename) {
        $filename = time();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use its parent directory name instead.
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    // Suffix some random data to avoid filename conflicts
    $temp_filename .= '-' . wp_generate_password(6, false);
    $temp_filename .= '.tmp';
    $temp_filename = $dir . wp_unique_filename($dir, $temp_filename);
    $fp = @fopen($temp_filename, 'x');
    if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
        return wp_tempnam($filename, $dir);
    }
    if ($fp) {
        fclose($fp);
    }
    return $temp_filename;
}

WordPress Version: 4.4

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || '.' == $filename || '/' == $filename) {
        $filename = time();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use its parent directory name instead.
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    // Suffix some random data to avoid filename conflicts
    $temp_filename .= '-' . wp_generate_password(6, false);
    $temp_filename .= '.tmp';
    $temp_filename = $dir . wp_unique_filename($dir, $temp_filename);
    $fp = @fopen($temp_filename, 'x');
    if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
        return wp_tempnam($filename, $dir);
    }
    if ($fp) {
        fclose($fp);
    }
    return $temp_filename;
}

WordPress Version: .10

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || '.' == $filename || '/' == $filename) {
        $filename = time();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use it's parent directory name instead
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    $temp_filename .= '.tmp';
    $temp_filename = $dir . wp_unique_filename($dir, $temp_filename);
    touch($temp_filename);
    return $temp_filename;
}

WordPress Version: 4.2

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    if (empty($filename) || '.' == $filename) {
        $filename = time();
    }
    // Use the basename of the given file without the extension as the name for the temporary directory
    $temp_filename = basename($filename);
    $temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
    // If the folder is falsey, use it's parent directory name instead
    if (!$temp_filename) {
        return wp_tempnam(dirname($filename), $dir);
    }
    $temp_filename .= '.tmp';
    $temp_filename = $dir . wp_unique_filename($dir, $temp_filename);
    touch($temp_filename);
    return $temp_filename;
}

WordPress Version: 4.1

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 * @param string $dir      Optional. Directory to store the file in. Default empty.
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    $filename = basename($filename);
    if (empty($filename)) {
        $filename = time();
    }
    $filename = preg_replace('|\..*$|', '.tmp', $filename);
    $filename = $dir . wp_unique_filename($dir, $filename);
    touch($filename);
    return $filename;
}

WordPress Version: 3.7

/**
 * Returns a filename of a Temporary unique file.
 * Please note that the calling function must unlink() this itself.
 *
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 *
 * @since 2.6.0
 *
 * @param string $filename (optional) Filename to base the Unique file off
 * @param string $dir (optional) Directory to store the file in
 * @return string a writable filename
 */
function wp_tempnam($filename = '', $dir = '')
{
    if (empty($dir)) {
        $dir = get_temp_dir();
    }
    $filename = basename($filename);
    if (empty($filename)) {
        $filename = time();
    }
    $filename = preg_replace('|\..*$|', '.tmp', $filename);
    $filename = $dir . wp_unique_filename($dir, $filename);
    touch($filename);
    return $filename;
}