Mercurial > hg > webaudioevaluationtool
annotate save.php @ 350:b448ed356355 Dev_main
Feature #1456. PHP server adds a six character 'hash' to the end of the file.
author | Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk> |
---|---|
date | Mon, 23 Nov 2015 16:02:37 +0000 |
parents | e40eb1c67933 |
children | 9823ee8c823e |
rev | line source |
---|---|
n@292 | 1 <?php |
b@343 | 2 header('Access-Control-Allow-Origin: *'); |
nicholas@345 | 3 header("Content-type: text/xml"); |
n@292 | 4 $postText = file_get_contents('php://input'); |
n@292 | 5 $datetime = date('ymdHis'); |
nicholas@350 | 6 $xmlfile = "save".$datetime."-".generateRandomString(6).".xml"; |
n@292 | 7 $fileHandle = fopen("saves/".$xmlfile, 'w'); |
nicholas@345 | 8 if ($fileHandle == FALSE) |
nicholas@345 | 9 { |
nicholas@345 | 10 // Filehandle failed |
nicholas@345 | 11 $xml = '<response state="error"><message>Could not open file</message></response>'; |
nicholas@345 | 12 echo $xml; |
nicholas@345 | 13 return; |
nicholas@345 | 14 } |
nicholas@345 | 15 $wbytes = fwrite($fileHandle, $postText); |
nicholas@345 | 16 if ($wbytes == FALSE) |
nicholas@345 | 17 { |
nicholas@345 | 18 // FileWrite failed |
nicholas@345 | 19 $xml = '<response state="error"><message>Could not write file "saves/'.$xmlfile.'"</message></response>'; |
nicholas@345 | 20 echo $xml; |
nicholas@345 | 21 return; |
nicholas@345 | 22 } |
n@292 | 23 fclose($fileHandle); |
nicholas@345 | 24 |
nicholas@345 | 25 // Return JSON confirmation data |
nicholas@345 | 26 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"saves/'.$xmlfile.'"</file></response>'; |
nicholas@345 | 27 echo $xml; |
nicholas@350 | 28 |
nicholas@350 | 29 // Random String generator from http://stackoverflow.com/questions/4356289/php-random-string-generator |
nicholas@350 | 30 function generateRandomString($length = 10) { |
nicholas@350 | 31 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
nicholas@350 | 32 $charactersLength = strlen($characters); |
nicholas@350 | 33 $randomString = ''; |
nicholas@350 | 34 for ($i = 0; $i < $length; $i++) { |
nicholas@350 | 35 $randomString .= $characters[rand(0, $charactersLength - 1)]; |
nicholas@350 | 36 } |
nicholas@350 | 37 return $randomString; |
nicholas@350 | 38 } |
n@292 | 39 ?> |