0, 'images' => [], 'nonImages' => [], 'error' => ''];
foreach ($_FILES['files']['name'] as $i => $originalName) {
if ($_FILES['files']['error'][$i] !== 0) continue;
$tmp = $_FILES['files']['tmp_name'][$i];
$size = $_FILES['files']['size'][$i];
$ext = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
if ($size > $allowedMaxSize) {
$response['error'] = "File $originalName terlalu besar (max 50MB).";
continue;
}
$isImage = in_array($ext, $imageExtensions);
if ($isImage) {
$newName = bin2hex(random_bytes(12)) . '.' . $ext;
} else {
$sanitized = preg_replace('/[^a-zA-Z0-9._-]/', '_', $originalName);
$newName = $sanitized;
$counter = 1;
$base = pathinfo($sanitized, PATHINFO_FILENAME);
$extPart = pathinfo($sanitized, PATHINFO_EXTENSION);
while (file_exists($uploadDir . $newName)) {
$newName = $base . '_' . $counter . ($extPart ? '.' . $extPart : '');
$counter++;
}
}
$dest = $uploadDir . $newName;
if (move_uploaded_file($tmp, $dest)) {
$response['uploaded']++;
$url = $baseUrl . $dest;
$fileData = [
'name' => $newName,
'url' => $url,
'size' => filesize($dest),
'date' => filemtime($dest),
'ext' => $ext,
'isImage' => $isImage
];
if ($isImage && $ext !== 'svg') {
$imgSize = @getimagesize($dest);
if ($imgSize) {
$fileData['width'] = $imgSize[0];
$fileData['height'] = $imgSize[1];
}
}
if ($isImage) {
$response['images'][] = $fileData;
} else {
$response['nonImages'][] = $fileData;
}
}
}
header('Content-Type: application/json');
echo json_encode($response);
exit;
}
// === DELETE ===
if ($action === 'delete' && isset($_GET['file'])) {
$file = $uploadDir . basename($_GET['file']);
if (file_exists($file) && is_file($file)) {
unlink($file);
}
header("Location: ?");
exit;
}
// === LIST GAMBAR SAJA ===
$images = [];
if (is_dir($uploadDir)) {
foreach (array_diff(scandir($uploadDir), ['.', '..']) as $f) {
$path = $uploadDir . $f;
if (is_file($path)) {
$ext = strtolower(pathinfo($f, PATHINFO_EXTENSION));
if (in_array($ext, $imageExtensions)) {
$url = $baseUrl . $path;
$size = filesize($path);
$date = filemtime($path);
$width = $height = null;
if ($ext !== 'svg') {
$imgSize = @getimagesize($path);
if ($imgSize) {
$width = $imgSize[0];
$height = $imgSize[1];
}
}
$images[] = [
'name' => $f,
'path' => $path,
'url' => $url,
'size' => $size,
'date' => $date,
'ext' => $ext,
'isImage' => true,
'width' => $width,
'height' => $height
];
}
}
}
usort($images, fn($a, $b) => $b['date'] - $a['date']);
}
?>
IMAGE UPLOADER
Daftar Gambar ( )
Preview
Nama
Ukuran
Dimensi
Upload
Link
Hapus
acak
COPY
Belum ada gambar diupload.
×