WordPress.org

Make WordPress Core

Opened 19 months ago

Last modified 9 months ago

#38560 new feature request

Add support for uploading arrayed $_FILES

Reported by: kontur Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Media Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description (last modified by swissspidy)

When you have form code that references [several file uploads](http://php.net/manual/en/features.file-upload.multiple.php) as array, the media_handle_upload() function fails to handle this. For example this:

<input type="file" name="documents[]">
<input type="file" name="documents[]">

Will result in a $_FILES array that's creates subarrays shaped like so:

$_FILES['userfile']['name'][0]
$_FILES['userfile']['name'][1]
$_FILES['userfile']['tmp_name'][0]
$_FILES['userfile']['tmp_name'][1]
$_FILES['userfile']['size'][0]
$_FILES['userfile']['size'][1]
$_FILES['userfile']['type'][0] 
$_FILES['userfile']['type'][1]

This will fail when passed to media_handle_upload, since the implementation does not handle this array structure nor is this documented.

Change History (5)

#1 @kontur
19 months ago

The obvious but unelegant workaround is to name each upload file input with an index or the like, and manually pick them apart before passing them separately to the media_upload_handle()

#2 @swissspidy
19 months ago

  • Description modified (diff)
  • Version 4.6.1 deleted

This ticket was mentioned in Slack in #core-media by desrosj. View the logs.


9 months ago

#4 @desrosj
9 months ago

  • Type changed from defect (bug) to enhancement

#5 @joemcgill
9 months ago

  • Keywords needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release
  • Summary changed from media_handle_upload() can't handle arrayed $_FILES to Add support for uploading arrayed $_FILES
  • Type changed from enhancement to feature request

Hi @kontur,

Thanks for the ticket.

The media_handle_upload function is meant to handle a single file at a time, from which it will generate and return a single attachment. If you're writing custom code, I would suggest handling the $_FILES object yourself, as you are suggesting.

See examples: https://wordpress.stackexchange.com/questions/173197/upload-multiple-files-with-media-handle-upload

If we want to support this in WordPress core, we should add handling for multiple file outside media_handle_upload, in places like wp_ajax_upload_attachment and WP_REST_Attachments_Controller->upload_from_file().

Note: See TracTickets for help on using tickets.