diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 8989026c22..67386aaa3c 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -1918,18 +1918,19 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) // Check for both lower and upper case extension or image sub-sizes may be overwritten. while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) { $new_number = $number + 1; - $filename = str_replace( "$number$ext", "$new_number$ext", $filename ); - $filename2 = str_replace( "$number$ext2", "$new_number$ext2", $filename2 ); + $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-$new_number$ext", $filename ); + $filename2 = str_replace( array( "-$number$ext2", "$number$ext2" ), "-$new_number$ext2", $filename2 ); $number = $new_number; } return $filename2; } while ( file_exists( $dir . "/$filename" ) ) { - if ( '' == "$number$ext" ) - $filename = $filename . ++$number . $ext; - else - $filename = str_replace( "$number$ext", ++$number . $ext, $filename ); + if ( '' == "$number$ext" ) { + $filename = "$filename-" . ++$number; + } else { + $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-" . ++$number . $ext, $filename ); + } } } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 1119770c97..631b303e70 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -146,8 +146,8 @@ class Tests_Functions extends WP_UnitTestCase { // check number is appended for file already exists $this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' ); - $this->assertEquals( 'test-image1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'Number not appended correctly' ); - $this->assertFileNotExists( $testdir . 'test-image1.png' ); + $this->assertEquals( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'Number not appended correctly' ); + $this->assertFileNotExists( $testdir . 'test-image-1.png' ); // check special chars $this->assertEquals( 'testtést-imagé.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' );