Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PATCH/PUT not accepting multipart/form-data file uploads?

Any idea why PATCH and PUT wouldn't be accepting multipart/form-data file uploads?

When I run var_dump($_FILES) it outputs array(0) { }. Any ideas why this is happening? If I POST the file, it works fine.

Below is an example of the request I am running.

Thanks in advance!

PUT /test.php HTTP/1.1
Content-Type: multipart/form-data; boundary=__X_PAW_BOUNDARY__
Host: [redacted]
Connection: close
User-Agent: Paw/2.1.1 (Macintosh; OS X/10.10.2) GCDHTTPRequest
Content-Length: 17961

--__X_PAW_BOUNDARY__
Content-Disposition: form-data; name="avatar"; filename="default.png"
Content-Type: image/png

PNG


[IMAGE DATA]
--__X_PAW_BOUNDARY__--
like image 787
Zachary Christopoulos Avatar asked Mar 01 '15 07:03

Zachary Christopoulos


1 Answers

When uploading files using a PUT request you don't use multipart/form-data. A PUT request is almost the same as a GET request. All you should be doing is putting the contents of the file in the body of the request. After that you can retrieve the file with the following code as explained in the php docs:

http://php.net/manual/en/features.file-upload.put-method.php):

<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time
   and write to the file */
while ($data = fread($putdata, 1024))
  fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>
like image 105
NoCode Avatar answered Nov 12 '22 14:11

NoCode