receive xml file via post in php
Your method is fine, and by the looks of it, the proper way to do it, with some notes:
- If you have PHP5, you can use
file_put_contents
as the inverse operation offile_get_contents
, and avoid the wholefopen/fwrite/fclose
. However: If the XML POST bodies you will be accepting may be large, your code right now may run into trouble. It first loads the entire body into memory, then writes it out as one big chunk. That is fine for small posts but if the filesizes tend into megabytes it would be better do to it entirely with
fopen/fread/fwrite/fclose
, so your memory usage will never exceed for example 8KB:$inp = fopen("php://input");$outp = fopen("xmlfile" . date("YmdHis") . ".xml", "w");while (!feof($inp)) { $buffer = fread($inp, 8192); fwrite($outp, $buffer);}fclose($inp);fclose($outp);
Your filename generation method may run into name collissions when files are posted more regularly than 1 per second (for example when they are posted from multiple sources). But I suspect this is just example code and you are already aware of that.