Parte 2/2
file : save_load.php
Codice PHP:
< !DOCTYPE html>
< html>
< head>
< title><?php
if (!isset($_GET['action'])) {
$_GET['action'] = 'load';
}
$action = $_GET['action'];
if ($_GET['action'] == 'save') {
print "Save Canvas";
} else {
print "Load Canvas";
}
?></title>
< script language="JavaScript" type="text/javascript">
< !--
function save() {
if (controlFileName()) {
document.getElementById('usefile').value = document.getElementById('filename').value;
document.getElementById('tosave').value = "";
listOfPointsX = new Array();
listOfPointsY = new Array();
var i = 0;
for(i=0;i<window.opener.points;i++) {
document.getElementById('tosave').value = document.getElementById('tosave').value + window.opener.arrPX[i]+','+window.opener.arrPY[i]+'|';
}
document.form1.submit();
}
} // function save()
function load() {
if (controlFileName()) {
document.getElementById('usefile').value = document.getElementById('filename').value;
document.form1.submit();
}
} // function load()
function controlFileName() {
var str = document.getElementById('filename').value;
if (str == "") {
alert("Digitare o cliccare il nome di un file");
return false;
}
var re = /(\.upo)$/i;
found = str.search(re);
if (found == -1) {
alert("Non è un file .upo");
return false;
}
return true;
} // function controlFileName()
//-->
< /script>
< /head>
< body style="font-family:tahoma"><?php
if (isset($_POST['go'])) {
if ($action == 'save') {
file_put_contents($_POST['usefile'], $_POST['tosave']);
} else {
$str = file_get_contents($_POST['usefile']);
$array = explode('|',$str);?>
<script language="javascript">
window.opener.clearCanvas();<?php
foreach($array as $value) {
if ($value != "") {
$array2 = explode(',',$value);?>
window.opener.addPoint(<?php print $array2[0] ?>,<?php print $array2[1] ?>);<?php
}
}?>
window.opener.showPointsList();
</script><?php
}?>
<script language="javascript">
window.close();
</script><?php
} // if (isset($_POST['go']))
?>
File name =
<input type="input" id="filename" size="40" maxlength="80"/><?php
if ($_GET['action'] == 'save') {?>
<input type="button" name="filename" value="Salva" onclick="save()"/><?php
} else {?>
<input type="button" name="filename" value="Carica" onclick="load()"/><?php
}?>
< hr/>
I nomi dei files devono finore con .upo
< hr/>
< form action="save_load.php?action=<?php print $action ?>" method="post" name="form1"/>
<input type="hidden" name="tosave" id="tosave"/>
<input type="hidden" name="go" id="go" value="1"/>
<input type="hidden" name="usefile" id="usefile"/>
< /form>
< table summary="">
< ?php
//-------------- Liste des fichiers
$directory = "./";
$extension = ".upo";
$at_least_1 = 0;
$open= @OpenDir($directory);
while($curfile=@ReadDir($open)) {
if(($curfile!=".")&&($curfile!="..") && ! is_dir($directory."/".$curfile)) {
if (mb_eregi("($extension)", $curfile)) {
$at_least_1 = 1;?>
<tr onmouseover="this.style.backgroundColor='red';this.style.color='white';" on
onmouseout="this.style.backgroundColor='white';this.style.color='black';" >
<td style="font-family:tahoma; font-size:10pt" onclick="document.getElementById('filename').value='<?php print $curfile ?>'">
<?php print $curfile ?>
</td>
</tr><?php
} // if mb_eregi(".mag",$curfile)
} // if(($curfile!=".")&&($curfile!=".."))
} // while($curfile=ReadDir($open))
//-------------- Aucun fichier;
if ($at_least_1 == 0) {?>
<tr>
<td style="font-family:tahoma; font-size:12pt">
No upo file ...
</td>
</tr><?php
} // if ($at_least_1 == 0)
?>
< /table>
< /body>
< /html>