HEX
Server: Apache/2.4.54 (Win64) OpenSSL/1.1.1q PHP/8.1.10
System: Windows NT ALTAIR 10.0 build 20348 (Windows Server 2022) AMD64
User: Administrator (0)
PHP: 8.1.10
Disabled: NONE
Upload Files
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']);
}