annotate save.php @ 814:22ad83e232f7

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