(CURL, PHP) Uploading files with PUT request, How do I process this?

后端 未结 2 835
执笔经年
执笔经年 2021-01-21 23:51

this is what i have in process.php

parse_str(file_get_contents(\"php://input\"),$upload_data);
if (isset($upload_data)) {
   print_r($upload_data);
   exit;
}


        
相关标签:
2条回答
  • 2021-01-21 23:56

    Short answer

    The only way in your situation is to parse raw request manually (look at this gist)

    Thoughts

    • PUT request could be used to upload one file only (not from html upload form, but using custom request). On the server you just get raw file content and writes it into some file.
    • Use PUT without files (application/x-www-form-urlencoded) and parse raw request using parse_str(file_get_contents("php://input"), $_PUT);
    • Use POST to upload files (multipart/form-data).
    0 讨论(0)
  • 2021-01-22 00:03

    The $_FILES array being populated on PUT requests. That's because a PUT request will specify the file name in the url and the file content in the body. Nothing more.

    You'll have to use php://input as you already suggested.

    upload.php

    <?php
    /* PUT data goes to php://input */
    echo file_get_contents("php://input");
    

    Then use the following curl command line:

    curl --upload -H "X-TOKEN: test123" a.txt  http://localhost/upload.php
    

    Update after comments: Using the --upload option should fit your needs. In difference to -X PUT together with -F, the data will be send raw and get not multipart/form-data encoded.. One of the hidden treasures of curl ;)

    0 讨论(0)
提交回复
热议问题