Make WordPress Core

Opened 16 months ago

Last modified 15 months ago

#63509 new enhancement

Use proper filename for attachment post title

Reported by: rilwis Owned by:
Priority: normal Milestone: Awaiting Review
Component: Media Version:
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

In media_handle_upload() function, the file name (and thus, attachment title) is gotten from $_FILES[ $file_id ]['name']. This is the raw/original file name when uploaded. However, developers can change file name after upload by filtering to sanitize_file_name or use unique_filename_callback parameter. This makes the file name (and title) doesn't represent the correct file name after upload.

$name = $_FILES[ $file_id ]['name'];
$ext  = pathinfo( $name, PATHINFO_EXTENSION );
$name = wp_basename( $name, ".$ext" );

$url     = $file['url'];
$type    = $file['type'];
$file    = $file['file'];
$title   = sanitize_text_field( $name );

A simple patch for this is this:

$name = $file['file']; // Get the proper name
$ext  = pathinfo( $name, PATHINFO_EXTENSION );
$name = wp_basename( $name, ".$ext" );

I tested the patch with classic editor and it works well. However, I haven't figured out for the block editor.

Attachments (1)

63509.diff (739 bytes ) - added by rilwis 16 months ago.
Patch for use proper file name after upload

Download all attachments as: .zip

Change History (2)

@rilwis
16 months ago

Patch for use proper file name after upload

#1 @Presskopp
15 months ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.