File: C:/laragon/www/dropbymatte/delete-file.php
<?php
session_start();
header('Content-Type: application/json');
require 'config.php';
if (!isset($_SESSION['admin'])) {
echo json_encode(['success' => false, 'error' => 'Sessione scaduta']);
exit;
}
$id = $_POST['id'] ?? '';
if (!$id) {
echo json_encode(['success' => false, 'error' => 'ID mancante']);
exit;
}
try {
// find filepath
$stmt = $pdo->prepare("SELECT filepath FROM files WHERE id = ?");
$stmt->execute([$id]);
$row = $stmt->fetch();
if ($row) {
$filepath = $row['filepath'];
// delete file
if (file_exists($filepath)) {
unlink($filepath);
}
// delete record
$stmt = $pdo->prepare("DELETE FROM files WHERE id = ?");
$stmt->execute([$id]);
}
echo json_encode(['success' => true]);
} catch (Exception $e) {
error_log("delete-file error: " . $e->getMessage());
echo json_encode(['success' => false, 'error' => 'Errore interno']);
}