Curl PUT request with file upload to PHP Curl PUT request with file upload to PHP curl curl

Curl PUT request with file upload to PHP


I have some ideas for debugging.

Do a var_dump(file_get_contents('php://input')); instead of an echo. According to the reference:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

If you get a bool(false) as output, there's something wrong which makes you cannot read php://input - most likely a PHP issue. If you get string(0) "", there's just nothing in php://input (anymore?), which makes it more likely that this is an nginx issue.

Also, according to the php:// reference, you cannot use php://input with enctype="multipart/form-data". Are you sure you don't use that one? You could also try an HTML file if that's more familiar.

You can also check the error logs, /var/log/nginx/error.log by default. Also, check the HTTP response code. Is it 200? If not, is it a helpful code?


The option -F you were using causes curl to POST data using the Content-Type multipart/form-data (see man curl).

You can use the --data-binary option like in:

curl -X PUT --data-binary "@avatar.jpg" http://api.test.com/user/dsadasdsa

The -d option is only for text and may corrupt your data.

In my tests, the following command gives the same results.

curl --upload "avatar.jpg" http://api.test.com/user/dsadasdsa

Here is my server

<?phpecho "request method : " . $_SERVER['REQUEST_METHOD'] . "\n";echo "dump files ";var_dump($_FILES);$putdata = fopen("php://input", "r");while ($data = fread($putdata, 1024)) {      echo $data . "\n";}?>


Nginx supports all http verbs (put, delete, options, etc). So you don't need any special setup in nginx for that.

The following works (I am using php5.4 server to easily test this):

Add this simple testing php script called put.php

$putdata = fopen("php://input", "r");while ($data = fread($putdata, 1024)) {      echo $data . "\n";}

Start a php server from the same folder of put.php:

php -S localhost:5000

Issue a PUT curl request:

curl -d "@/path/to/put.php" -X PUT http://localhost:5000/put.php

This will print the file content of put.php

[update]

In case you want to use php to send a curl request to a restful api site, I'd recommend Guzzle, with which you don't have to remember all of those curl options.