[wp-trac] [WordPress Trac] #21679: media_handle_upload does not provide a way to change the file's name

WordPress Trac wp-trac at lists.automattic.com
Fri Aug 24 07:02:02 UTC 2012


#21679: media_handle_upload does not provide a way to change the file's name
-------------------------------------------------+-------------------------
 Reporter:  Willshouse                           |       Owner:
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
Component:  Media                                |  Review
 Severity:  normal                               |     Version:  2.5
 Keywords:  has-patch needs-testing dev-         |  Resolution:
  feedback                                       |
-------------------------------------------------+-------------------------
Description changed by johnbillion:

Old description:

> `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.
>
> [[Image(http://img24.imageshack.us/img24/3679/pictureyp.png)]]
>
> 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'] );

New description:

 `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.

 [[Image(http://img24.imageshack.us/img24/3679/pictureyp.png)]]

 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'] );
 }}}

--

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21679#comment:2>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list