-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstorage_handler.php
More file actions
53 lines (45 loc) · 1.64 KB
/
storage_handler.php
File metadata and controls
53 lines (45 loc) · 1.64 KB
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
// the gist of the algorithm
// if the request method is 'SPECIAL' then user is sending data to the server
// we take everything on the request body and dump it to data.txt
// else user is retrieving data
// we read data.txt and transfer that over
$filename = "data.txt";
//----[saving data]--------------------------------------------------
if ($_SERVER['REQUEST_METHOD'] === 'SPECIAL') {
if (file_exists($filename)) { // rename file to avoid loosing it on error
if(!rename( $filename, $filename.".bak")) {
http_response_code(500);
die();
}
}
if ($fp = fopen('data.txt', 'w')) {
fwrite($fp, file_get_contents('php://input') );
fclose($fp);
echo "Last save: ".date("Ymd H:i:s")."\0"; // on success we repond with a time stamp
} else {
http_response_code(500);
die();
}
if (file_exists($filename.".bak")) { // delete old file
unlink($filename.".bak");
}
die();
}
//----[loading data]--------------------------------------------------
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
flush(); // Flush system output buffer
readfile($filename);
die();
} else {
http_response_code(404);
die();
}
?>