0

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.

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • So what have you tried so far? – hindmost Mar 17 '14 at 09:01
  • I don't have a clue on how to start, so how could I have tried anything? :/ I could show you useless code that can receive text and store it in a text file, but how would that help... – Forivin Mar 17 '14 at 09:07
  • As far as I understood you need to pass additional data (path to save a file) besides binary file's content. You may use custom HTTP header for this purpose. – hindmost Mar 17 '14 at 09:27

1 Answers1

1

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.

Community
  • 1
  • 1
Loopo
  • 2,204
  • 2
  • 28
  • 45
  • I don't see the common use of POST as an advantage. That's why I'm asking here. What I want to do with the files (most of them will be image files like png, gif and jpg) is saving them on the server and allow people to access them with certain GET requests. I don't know if it needs to be restful or w/e. – Forivin Mar 17 '14 at 17:15