WordPress.org

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

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.

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

Attachments (3)

media.patch (381 bytes) - added by Willshouse 20 months ago.
patch
Picture.png (28.8 KB) - added by Willshouse 20 months ago.
media upload screnshot
21679.patch (564 bytes) - added by SergeyBiryukov 20 months ago.

Download all attachments as: .zip

Change History (9)

Willshouse20 months ago

patch

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

comment:3 SergeyBiryukov20 months 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.

comment:4 jsdavis029 months 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

comment:5 jsdavis029 months ago

  • Cc jsdavis02 added
  • Keywords needs-patch added; has-patch removed

comment:6 danielbachhuber7 weeks 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()

Note: See TracTickets for help on using tickets.