getConnection(); if (empty($_POST['production_id']) || empty($_POST['photo_type'])) { throw new Exception("Dati mancanti"); } $production_id = (int)$_POST['production_id']; $type = $_POST['photo_type']; if (!isset($_FILES['photo'])) { throw new Exception("Nessuna foto caricata"); } // Estensione $ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION); $ext = strtolower($ext ?: "jpg"); // Inserimento record DB $stmt = $pdo->prepare(" INSERT INTO production_photos (production_id, photo_type, filename) VALUES (?, ?, '') "); $stmt->execute([$production_id, $type]); $photo_id = $pdo->lastInsertId(); $timestamp = time(); $filename = "{$production_id}-{$photo_id}-{$timestamp}.{$ext}"; $filepath = __DIR__ . "/photos/" . $filename; if (!move_uploaded_file($_FILES['photo']['tmp_name'], $filepath)) { throw new Exception("Errore salvataggio file"); } // aggiorna filename completo $pdo->prepare(" UPDATE production_photos SET filename = ? WHERE id = ? ")->execute([$filename, $photo_id]); echo json_encode(["success" => true]); } catch (Exception $e) { echo json_encode([ "success" => false, "message" => $e->getMessage() ]); }