Make WordPress Core

Opened 20 months ago

Last modified 7 weeks ago

#21679 new defect (bug)

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

Reported by: Willshouse Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.5
Component: Media Keywords: needs-testing dev-feedback needs-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)

Willshouse20 months ago


Willshouse20 months ago

media upload screnshot

comment:1 c3mdigital20 months 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 johnbillion20 months ago

  • Description modified (diff)

SergeyBiryukov20 months ago

Note: See TracTickets for help on using tickets.