Handling PUT request in php

Handling PUT Requests

To uploading a file via put request is simpler than post.


$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time and write to the file */
while ($data = fread($putdata, 1024))
  fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);

Here put request data will always comes in tmp file. then we read 1kb chunk of data and write it into a new file.
OR we can use file_get_contents/file_put_contents to read and write from tmp file to new file.


$putdata = file_get_contents("php://input");
file_put_contents("myputfile.ext", $putdata);