comparison save.php @ 815:3b0770e1e616

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