63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
require_once('include/headscript.php');
|
|
|
|
/**
|
|
* Upload copertina evento (chiamato in AJAX dalla dropzone di event_form.php).
|
|
* Valida tipo/dimensione, salva in assets/uploads/events/ e ritorna JSON:
|
|
* { ok:true, path:"assets/uploads/events/xxxx.jpg" } oppure { ok:false, error:"..." }
|
|
*/
|
|
if (function_exists('ob_get_level') && ob_get_level()) {
|
|
ob_clean(); // pulisce eventuali whitespace dell'include prima del JSON
|
|
}
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
$fail = function (string $msg): void {
|
|
echo json_encode(['ok' => false, 'error' => $msg]);
|
|
exit();
|
|
};
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_FILES['cover'])) {
|
|
$fail('Richiesta non valida.');
|
|
}
|
|
|
|
$file = $_FILES['cover'];
|
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
$fail('Errore nel caricamento (codice ' . (int) $file['error'] . ').');
|
|
}
|
|
if ($file['size'] > 5 * 1024 * 1024) {
|
|
$fail("L'immagine supera i 5 MB.");
|
|
}
|
|
|
|
// Validazione MIME reale (non ci si fida dell'estensione)
|
|
$allowed = ['image/jpeg' => 'jpg', 'image/png' => 'png', 'image/webp' => 'webp', 'image/gif' => 'gif'];
|
|
$info = @getimagesize($file['tmp_name']);
|
|
$mime = $info['mime'] ?? '';
|
|
if (!isset($allowed[$mime])) {
|
|
$fail('Formato non supportato. Usa JPG, PNG, WEBP o GIF.');
|
|
}
|
|
$ext = $allowed[$mime];
|
|
|
|
$relDir = 'assets/uploads/events';
|
|
$absDir = __DIR__ . '/' . $relDir;
|
|
if (!is_dir($absDir)) {
|
|
@mkdir($absDir, 0775, true);
|
|
}
|
|
if (!is_dir($absDir) || !is_writable($absDir)) {
|
|
$fail('Cartella non scrivibile: ' . $relDir . ' (crea la cartella e dai permessi di scrittura).');
|
|
}
|
|
|
|
try {
|
|
$rand = bin2hex(random_bytes(6));
|
|
} catch (Throwable $e) {
|
|
$rand = uniqid('', true);
|
|
}
|
|
$name = date('Ymd_His') . '_' . $rand . '.' . $ext;
|
|
$dest = $absDir . '/' . $name;
|
|
|
|
if (!move_uploaded_file($file['tmp_name'], $dest)) {
|
|
$fail('Impossibile salvare il file.');
|
|
}
|
|
|
|
echo json_encode(['ok' => true, 'path' => $relDir . '/' . $name]);
|
|
exit();
|