annotate save.php @ 1452:bd5cbaf775cd

Test Create: Delete Buttons included on final checks
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Wed, 25 Nov 2015 09:26:10 +0000
parents
children 1b6fa37d46a4
rev   line source
nickjillings@1452 1 <?php
nickjillings@1452 2 header('Access-Control-Allow-Origin: *');
nickjillings@1452 3 header("Content-type: text/xml");
nickjillings@1452 4 $postText = file_get_contents('php://input');
nickjillings@1452 5 $datetime = date('ymdHis');
nickjillings@1452 6 $xmlfile = "save".$datetime."-".generateRandomString(6).".xml";
nickjillings@1452 7 $fileHandle = fopen("saves/".$xmlfile, 'w');
nickjillings@1452 8 if ($fileHandle == FALSE)
nickjillings@1452 9 {
nickjillings@1452 10 // Filehandle failed
nickjillings@1452 11 $xml = '<response state="error"><message>Could not open file</message></response>';
nickjillings@1452 12 echo $xml;
nickjillings@1452 13 return;
nickjillings@1452 14 }
nickjillings@1452 15 $wbytes = fwrite($fileHandle, $postText);
nickjillings@1452 16 if ($wbytes == FALSE)
nickjillings@1452 17 {
nickjillings@1452 18 // FileWrite failed
nickjillings@1452 19 $xml = '<response state="error"><message>Could not write file "saves/'.$xmlfile.'"</message></response>';
nickjillings@1452 20 echo $xml;
nickjillings@1452 21 return;
nickjillings@1452 22 }
nickjillings@1452 23 fclose($fileHandle);
nickjillings@1452 24
nickjillings@1452 25 // Return JSON confirmation data
nickjillings@1452 26 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"saves/'.$xmlfile.'"</file></response>';
nickjillings@1452 27 echo $xml;
nickjillings@1452 28
nickjillings@1452 29 // Random String generator from http://stackoverflow.com/questions/4356289/php-random-string-generator
nickjillings@1452 30 function generateRandomString($length = 10) {
nickjillings@1452 31 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
nickjillings@1452 32 $charactersLength = strlen($characters);
nickjillings@1452 33 $randomString = '';
nickjillings@1452 34 for ($i = 0; $i < $length; $i++) {
nickjillings@1452 35 $randomString .= $characters[rand(0, $charactersLength - 1)];
nickjillings@1452 36 }
nickjillings@1452 37 return $randomString;
nickjillings@1452 38 }
nickjillings@1452 39 ?>