WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp unique filename

Description

This function can be used before saving a file to make sure it has a filename that is sanitized and unique for the given directory.

If the filename is not unique, then a number will be added to the filename before the extension, and will continue adding numbers until the filename is unique.

The function wp_unique_filename is passed three parameters:

  • the first one is the directory
  • the second is the filename
  • the third is an optional callback function

The callback function allows the caller to use their own method to create unique file names.

If defined, the callback should take three arguments - directory, base filename, and extension - and return a unique filename.

Example

$newfilename = wp_unique_filename( $path_being_saved_to, $filename_to_check );

Source File

wp_unique_filename() is located in wp-includes/functions.php.

See also index of Function Reference and index of Template Tags.