Prevent uploads from overwriting each other. props neodude. fixes #6294

git-svn-id: http://svn.automattic.com/wordpress/trunk@7440 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
markjaquith 2008-03-21 04:03:33 +00:00
parent 44c9c17fe4
commit c5e0b789f6
1 changed files with 10 additions and 6 deletions

View File

@ -1123,26 +1123,30 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = NULL )
$ext = $info['extension'];
$name = basename($filename, ".{$ext}");
// edge case: if file is named '.ext', treat as an empty name
if( $name === ".$ext" )
$name = '';
// Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
if ( $unique_filename_callback && function_exists( $unique_filename_callback ) ) {
$filename = $unique_filename_callback( $dir, $name );
} else {
$number = '';
$filename = str_replace( '#', '_', $name );
$filename = str_replace( array( '\\', "'" ), '', $filename );
if ( empty( $ext) )
if ( empty( $ext ) )
$ext = '';
else
$ext = strtolower( ".$ext" );
$filename = $filename . $ext;
$filename = str_replace( $ext, '', $filename );
$filename = sanitize_title_with_dashes( $filename ) . $ext;
while ( file_exists( $dir . "/$filename" ) ) {
if ( '' == "$number$ext" )
$filename = $filename . ++$number . $ext;
else
$filename = str_replace( "$number$ext", ++$number . $ext, $filename );
}
$filename = str_replace( $ext, '', $filename );
$filename = sanitize_title_with_dashes( $filename ) . $ext;
}
return $filename;