WordPress.org

Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#41774 new defect (bug)

REST-API: Media endpoint (POST): Unable to upload files

Reported by: tiger42 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8.1
Component: REST API Keywords: dev-feedback
Focuses: rest-api Cc:

Description

I have found a possible bug in the file "wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php", which prevents me from uploading media files via the "media" REST endpoint. Every time I try to upload a file, I get the following response:

{
   "code": "rest_upload_unknown_error",
   "message": "File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.",
   "data": {"status": 500}
}

The problem seems to be in line 719:

$file = wp_handle_upload( $files['file'], $overrides );

After some debugging, I have found out that the array in the $files variable does not have the format that seems to be expected. After changing the line to the following code, the file upload works for me without problems:

$file = reset( $files );
$file = wp_handle_upload( $file, $overrides );

Change History (1)

#1 @rmccue
3 months ago

  • Keywords dev-feedback added

Hi @tiger42, and thanks for the report. I don't think this is a bug in the code, although we might be missing some documentation here.

How are you sending the file to the REST API? There are two ways you can pass files to the API:

  1. Send the file's data in the request body (raw mode). This also requires setting the Content-Disposition header to pass the filename.
  2. Send the file in multipart mode (upload mode). This needs to be passed via the file parameter in a multipart/form-data request.

It appears that you're using upload mode here, but not setting the parameter name to file.

Note: See TracTickets for help on using tickets.