prepare('SELECT * FROM doc_storage WHERE id = ? LIMIT 1'); $stmt->execute([$id]); $file = $stmt->fetch(); if (!$file) { json_error(404, 'File not found'); } $allowed = false; $baseDir = null; if (!empty($file['idhome'])) { $allowed = user_owns_home($pdo, $user, (int) $file['idhome']); $baseDir = $config['homedocs_dir']; } elseif (!empty($file['owner_id'])) { $allowed = user_owns_owner($pdo, $user, (int) $file['owner_id']); $baseDir = $config['persondocs_dir']; } if (!$allowed) { json_error(403, 'No access to this file'); } $path = $baseDir . '/' . basename((string) $file['filename']); if (is_file($path)) { @unlink($path); } $pdo->prepare('DELETE FROM doc_storage WHERE id = ?')->execute([$id]); json_ok();