Opened 12 years ago
Last modified 6 years ago
#21679 new enhancement
media_handle_upload does not provide a way to change the file's name
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | 2.5 |
Component: | Media | Keywords: | has-patch |
Focuses: | Cc: |
Description (last modified by )
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'] );
Attachments (3)
Change History (18)
#3
follow-up:
↓ 7
@
12 years ago
media.patch looks reversed. Even with that corrected, it would cause notices, since wp_handle_upload()
result doesn't contain the name
key:
Notice: Undefined index: name in wp-admin/includes/media.php on line 215 Notice: Undefined index: extension in wp-admin/includes/media.php on line 221
21679.patch fixes the notices, but has one side effect.
Currently, if you upload "Picture.jpg" several times, the attachments will have "Picture" title.
With the patch, the titles would be "Picture", "Picture1", "Picture2", etc.
A workaround for the inability to change the title with wp_handle_upload_prefilter
would probably be to hook into add_attachment
and alter the title after the attachment has been uploaded.
#4
@
12 years ago
I just fixed this on an install of mine. Wilishouse did the same thing I initially did assuming we got back the actual file object instead of a modified associative array from file.php...
return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ), 'upload' );
So to still avoid changing two core files I just did a quick change to media.php to strip it from the full upload path, so it has a filename that ran through all sanitizing and filter calls.
//$name = $_FILES[$file_id]['name']; --wrong time to pull and wrong place to pull from, //allow handle upload and prefilter to run first $file = wp_handle_upload($_FILES[$file_id], $overrides, $time); $name = substr($file['file'],strrpos($file['file'],'/')+1);
It really should not be difficult to add this in and it should be pretty low impact
#6
@
11 years ago
Similarly, there's no way to filter the target destination for the file, which is set by $time
.
#16849 is related in the sense that all of these are requests to change the behavior of wp_handle_upload()
#7
in reply to:
↑ 3
@
11 years ago
Replying to SergeyBiryukov:
Currently, if you upload "Picture.jpg" several times, the attachments will have "Picture" title.
With the patch, the titles would be "Picture", "Picture1", "Picture2", etc.
I don't think that's a bad side effect, maybe better than naming them all the same.
21679.patch looks good to me.
patch