[wp-trac] [WordPress Trac] #42437: Thumbnails can overwrite other uploads if filename matches

WordPress Trac noreply at wordpress.org
Wed May 16 09:35:52 UTC 2018

#42437: Thumbnails can overwrite other uploads if filename matches
 Reporter:  Viper007Bond  |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Future Release
Component:  Upload        |     Version:  4.8.3
 Severity:  normal        |  Resolution:
 Keywords:  needs-patch   |     Focuses:

Comment (by azaozz):

 Replying to [comment:11 pbiron]:
 > ...doing so might cause UX disconnects for some users.

 Right, and generally there is no point in fixing one rare edge case and
 possibly introducing another.

 > The best way I can think of avoiding these kinds of conflicts is to add
 a short hash to filenames of intermediate image sizes

 We already make the file names of intermediate image sizes longer. As far
 as I remember there was a problem with exceeding the allowed file name
 length for certain filesystems. Adding another (possibly long) string to
 the file names may trigger that.

 How about [#comment:5 my other idea]: if the original file name ends with
 something like `-600x800.jpg` rename it and append `-1`, `-2`, etc. the
 same way we do for all other naming conflicts in WP.

 The (basic) code to implement this would be:
 if ( preg_match( '/.+-\n+x\n+$/', $file_name ) ) {
   // Always append `-1` to file names that can potentially match other
 subsize file names.
   $i = 1;
   $full_name = $file_name . "-$i" . $file_extension;

   while ( is_file( $uploads_dir . $full_name ) ) {
     $full_name = $file_name . "-$i" . $file_extension;

 This will give us file names like `sunset-150x150-1.jpg`, `sundown-
 150x150-2.jpg` or `here-comes-the-night-150x150-1.jpg`. These names are a
 lot more "readable" and close to the original than something like `sunset-
 150x150-27624fea676d86f7.jpg` for subsizes.

Ticket URL: <https://core.trac.wordpress.org/ticket/42437#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list