Make WordPress Core

Opened 3 years ago

Last modified 7 months ago

#21679 new enhancement

media_handle_upload does not provide a way to change the file's name — at Version 2

Reported by: Willshouse Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 2.5
Component: Media Keywords: has-patch
Focuses: Cc:

Description (last modified by johnbillion)

wp-admin/includes/media.php has two operations that I believe should be reversed. Basically, $name is set based on the name of the raw uploaded file ( $_FILES[$file_id]['name']), however in the wp_handle_upload function you are able to use wp_handle_upload_prefilter to adjust the file's name - but when after the wp_handle_upload returns the changes will not show up in the title of the media dialog field even though the file has been renamed properly:

	$name = $_FILES[$file_id]['name'];
	$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);

	if ( isset($file['error']) )
		return new WP_Error( 'upload_error', $file['error'] );

	$name_parts = pathinfo($name);
	$name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );

In short, uploading a file named Picture.png and changing the name to test3.png using the wp_handle_upload_prefilter filter does in fact allow the file's name to be changed before it is saved, but the "title" is then incorrectly displayed in the media uploader dialog box.


This would be relatively easy to fix using this patch:

--- media.php	2012-08-23 23:57:02.000000000 -0400
+++ media-patch.php	2012-06-06 12:00:08.000000000 -0400
@@ -209,8 +209,8 @@
 			$time = $post->post_date;
+	$name = $_FILES[$file_id]['name'];
 	$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
-	$name = $file['name'];
 	if ( isset($file['error']) )
 		return new WP_Error( 'upload_error', $file['error'] );

Change History (5)

@Willshouse3 years ago


@Willshouse3 years ago

media upload screnshot

comment:1 @c3mdigital3 years ago

  • Keywords dev-feedback added
  • Version changed from 3.4.1 to 2.5

@Wilishouse could you take a look at the patch. The diff seems off . Make sure you are at latest version of trunk. Make your changes then run the diff command from the root directory of the install.

Possibly related #18730, #16330

comment:2 @johnbillion3 years ago

  • Description modified (diff)

@SergeyBiryukov3 years ago

Note: See TracTickets for help on using tickets.