media_handle_upload does not provide a way to change the file's name — at Version 2
|Reported by:||Willshouse||Owned by:|
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'] );