-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.php
53 lines (46 loc) · 1.79 KB
/
data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//1. Save the data to default.json file:
if (isset($_POST['data']) && !empty($_POST['data'])) {
//1. Save the data to default.json file:
$draftLocation = './data/drafts/default.json';
$myfile = fopen($draftLocation, "w") or die("Unable to open file!");
$txt = json_encode(array('blocks' => json_decode($_POST['data'], true)), JSON_UNESCAPED_SLASHES);
fwrite($myfile, $txt);
fclose($myfile);
//2. Save the data to a new temporary file:
$draftLocationTemp = './data/temp/'.time().'.json';
$myfileTemp = fopen($draftLocationTemp, "w") or die("Unable to open file!");
fwrite($myfileTemp, $txt);
fclose($myfileTemp);
}
//2. Save the data to a custom file:
if (isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['filename']) && !empty($_POST['filename'])) {
$documentLocation = './data/documents/'.$_POST['filename'];
$myDocumentFile = fopen($documentLocation, "w") or die("Unable to open file!");
fwrite($myDocumentFile, $_POST['data']);
fclose($myDocumentFile);
}
//3. Reset the default.json file:
if (isset($_POST['reset']) && !empty($_POST['reset'])) {
unlink('./data/drafts/default.json');
}
//4. Reset the temp directory:
if (isset($_POST['resetTemp']) && !empty($_POST['resetTemp'])) {
deleteFilesInDirectory('./data/temp');
}
}
function deleteFilesInDirectory($dir) {
if (!file_exists($dir) || !is_dir($dir)) {
return false;
}
$items = array_diff(scandir($dir), array('.', '..'));
foreach ($items as $item) {
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_file($path)) {
unlink($path);
}
}
return true;
}
?>