This repository was archived by the owner on Aug 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
112 lines (95 loc) · 3.27 KB
/
Copy pathupload.php
File metadata and controls
112 lines (95 loc) · 3.27 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
require_once __DIR__ . '/token.php';
checkAccess();
$uploadDir = __DIR__ . '/upload/';
$imgDir = __DIR__ . '/img/';
const MAX_UPLOAD_BYTES = 10 * 1024 * 1024; // 10 MB
$allowedMimes = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp',
];
function createImageResourceFromData(string $path, string $mime) {
switch ($mime) {
case 'image/jpeg':
return imagecreatefromjpeg($path);
case 'image/png':
return imagecreatefrompng($path);
case 'image/gif':
return imagecreatefromgif($path);
case 'image/webp':
return function_exists('imagecreatefromwebp') ? imagecreatefromwebp($path) : null;
default:
return null;
}
}
function saveReencodedImage($imgResource, string $destPath, string $mime): bool {
// Re-encode to the same family; strip metadata implicitly
switch ($mime) {
case 'image/jpeg':
return imagejpeg($imgResource, $destPath, 90);
case 'image/png':
return imagepng($imgResource, $destPath, 6);
case 'image/gif':
return imagegif($imgResource, $destPath);
case 'image/webp':
return function_exists('imagewebp') ? imagewebp($imgResource, $destPath, 90) : false;
default:
return false;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (empty($_FILES['files'])) {
http_response_code(400);
echo json_encode(['error'=>'Keine Dateien ausgewählt']);
exit;
}
$uploaded = [];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
foreach ($_FILES['files']['tmp_name'] as $i => $tmp) {
// Basic upload errors and size checks
if (!isset($_FILES['files']['error'][$i]) || $_FILES['files']['error'][$i] !== UPLOAD_ERR_OK) {
continue;
}
if (!is_uploaded_file($tmp)) {
continue;
}
$size = $_FILES['files']['size'][$i] ?? 0;
if ($size <= 0 || $size > MAX_UPLOAD_BYTES) {
continue;
}
$mime = finfo_file($finfo, $tmp) ?: '';
$imgInfo = @getimagesize($tmp);
if (!$imgInfo || empty($allowedMimes[$mime])) {
continue;
}
$ext = $allowedMimes[$mime];
// Dateiname: Datum + Uhrzeit
$datePrefix = date('Ymd_His');
$finalName = $datePrefix . '.' . $ext;
// Konflikte vermeiden
$counter = 1;
while (file_exists($uploadDir . $finalName)) {
$finalName = $datePrefix . '_' . $counter . '.' . $ext;
$counter++;
}
// Re-encode image to drop metadata and enforce safe format
$imgResource = createImageResourceFromData($tmp, $mime);
if (!$imgResource) {
continue;
}
$uploadPath = $uploadDir . $finalName;
$imgPath = $imgDir . $finalName;
$okUpload = saveReencodedImage($imgResource, $uploadPath, $mime);
$okImg = $okUpload ? saveReencodedImage($imgResource, $imgPath, $mime) : false;
imagedestroy($imgResource);
if ($okUpload && $okImg) {
$uploaded[] = $finalName;
}
}
finfo_close($finfo);
header('Content-Type: application/json');
echo json_encode(['uploaded'=>$uploaded]);
exit;
}