Make WordPress Core

Opened 3 years ago

Last modified 9 months ago

#21679 new enhancement

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

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


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);

if ( isset($fileerror?) )

return new WP_Error( 'upload_error', $fileerror? );

Change History (2)

3 years ago


3 years ago

media upload screnshot

Note: See TracTickets for help on using tickets.