Forivin
Forivin

Reputation: 15508

Save binary request body as file

I wrote a program that reads a binary file into the RAM and then sends it using an HTTP request to my server. It uses the PUT method and the binary file is (in) the body. Now how can I tell my server to receive and safe the file in a folder? If possible without any additional libraries that I would need to download (unless it's more efficient).
I know, there are some similar threads to this one, but they either they where about receiving text or they were about doing it with libraries or there simply was no sufficient answer.

I'd also like to know, if it would be more efficient or smarter to use the POST method or any other instead of PUT.

Upvotes: 0

Views: 1752

Answers (1)

Loopo
Loopo

Reputation: 2195

You can get at the data by opening a stream to php://input, like so:

$datastr = fopen('php://input',rb);
if ($fp = fopen('outputfile.bin', "wb")){
    while(!feof($datastr)){
        fwrite($fp,fread($datastr,4096)) ;
    }
}

As to whether to use POST or anything else depends on what is happening with the data, and whether you care about being RESTful or such. See other questions/answers, indempotency.

The advantage I would see with using POST is that it's more commonly used (on most submission forms where you upload a file), and therefore has more support from within PHP and html.

Upvotes: 1

Related Questions