-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
43 lines (37 loc) · 1.34 KB
/
index.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
<?php
// Function to ensure the directory for the given IP address exists
function ensureDirectory($ipAddress) {
$directory = 'ClientData/' . $ipAddress;
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
return $directory;
}
// Function to generate a unique file name if a file with the same name already exists
function generateUniqueFileName($directory, $fileName) {
$filePath = $directory . '/' . $fileName;
$fileInfo = pathinfo($filePath);
$baseName = $fileInfo['filename'];
$extension = isset($fileInfo['extension']) ? '.' . $fileInfo['extension'] : '';
$counter = 1;
while (file_exists($filePath)) {
$filePath = $directory . '/' . $baseName . '_' . $counter . $extension;
$counter++;
}
return $filePath;
}
// Handle the file upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$ipAddress = $_SERVER['REMOTE_ADDR'];
$uploadDirectory = ensureDirectory($ipAddress);
$fileName = basename($_FILES['file']['name']);
$targetFilePath = generateUniqueFileName($uploadDirectory, $fileName);
if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFilePath)) {
echo "$fileName successfully uploaded";
} else {
echo "There was an error uploading the file.";
}
} else {
echo "No file uploaded.";
}
?>