From c5e0b789f60a348c17982b510b7fac6aceacfa63 Mon Sep 17 00:00:00 2001 From: markjaquith Date: Fri, 21 Mar 2008 04:03:33 +0000 Subject: [PATCH] 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 --- wp-includes/functions.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 8cc9e2fba9..23dab22310 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1122,27 +1122,31 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = NULL ) $info = pathinfo($filename); $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;