Make WordPress Core

Opened 12 years ago

Closed 9 years ago

#21453 closed enhancement (fixed)

Appending hyphen before numbers when creating a create unique filename in wp_unique_filename()

Reported by: husobj's profile husobj Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version: 2.5
Component: Upload Keywords: has-patch
Focuses: Cc:

Description

When WordPress appends a number to a file to create a unique filename, it just adds a number to the file.
ie If "photo.jpg" exists then "photo1.jpg" is created

I suggest that it should include a hyphen so the file would be called "photo-1.jpg"

The main reason is that if you upload files ending in numbers then then the new filename is less easy to read.
ie If "photo234.jpg" exists then "photo2341.jpg" is created

Would be nicer if it produced "photo234-1.jpg"

It's only cosmetic but it does make it easier to search for images in the uploads folder if you need to for any reason, especially if you have uploaded a load of images from a camera DSC123.jpg, DSC124.jpg etc

Attachments (2)

append.hyphen.wp.unique.filename.21453.diff (1.3 KB) - added by mikejolley 11 years ago.
Patch to append hyphens e.g. IMAGE-1.jpg instead of IMAGE1.jpg
21453.patch (943 bytes) - added by tyxla 9 years ago.
Altering Tests_Functions::test_wp_unique_filename() to support the hyphen prefix when creating unique filenames

Download all attachments as: .zip

Change History (9)

#1 @c3mdigital
11 years ago

  • Keywords needs-patch added
  • Version changed from 3.4.1 to 2.5

+1 for consistency with wp_unique_post_slug() and wp_uniquie_term_slug()

@mikejolley
11 years ago

Patch to append hyphens e.g. IMAGE-1.jpg instead of IMAGE1.jpg

#2 @mikejolley
11 years ago

I've attached a patch for this. It will ensure appended numbers are prepended with a hyphen.

Works with both the extension case logic, when extension is non existent, and regular filenames.

#3 @mikejolley
11 years ago

  • Keywords has-patch needs-testing added; needs-patch removed
  • Version 2.5 deleted

#4 @SergeyBiryukov
11 years ago

  • Component changed from Filesystem to Upload
  • Keywords 3.9-early added
  • Milestone changed from Awaiting Review to Future Release
  • Version set to 2.5

#5 @wonderboymusic
9 years ago

  • Keywords needs-unit-tests added; 3.9-early needs-testing removed
  • Milestone changed from Future Release to 4.4

@tyxla
9 years ago

Altering Tests_Functions::test_wp_unique_filename() to support the hyphen prefix when creating unique filenames

#6 @tyxla
9 years ago

  • Keywords needs-unit-tests removed

I think that in terms of unit tests, this only requires a modification to Tests_Functions::test_wp_unique_filename() in order to support the new unique naming scheme.

@wonderboymusic do you think there are additional cases that should be covered?

#7 @wonderboymusic
9 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 35276:

Formatting: when making unique filenames in wp_unique_filename() by adding an incrementing number, prefix it with a dash to disambiguate from files that end in numbers.

Updates unit tests.

Props mikejolley, tyxla.
Fixes #21453.

Note: See TracTickets for help on using tickets.