Opened 6 years ago
Closed 4 years ago
#41774 closed defect (bug) (invalid)
REST-API: Media endpoint (POST): Unable to upload files
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 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 (3)
This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.
4 years ago
Note: See
TracTickets for help on using
tickets.
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:
file
parameter in amultipart/form-data
request.It appears that you're using upload mode here, but not setting the parameter name to
file
.