-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_data.php
More file actions
39 lines (32 loc) · 1.02 KB
/
write_data.php
File metadata and controls
39 lines (32 loc) · 1.02 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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Allow cross-origin requests
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
if (empty($_POST['postresult']) || empty($_POST['postfile'])) {
die("Error: Missing data or filename");
}
// Get the posted data
$data = $_POST['postresult'];
$filename = $_POST['postfile'];
// Clean the filename to prevent directory traversal
$filename = basename($filename);
// Make sure we're writing to the data directory
$filepath = __DIR__ . "/data/" . $filename;
try {
// Create directory if it doesn't exist
if (!file_exists(__DIR__ . "/data")) {
mkdir(__DIR__ . "/data", 0777, true);
}
// Write the data
if (file_put_contents($filepath, $data, FILE_APPEND | LOCK_EX)) {
echo "Success: Data written to " . $filename;
} else {
echo "Error: Could not write to file";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>