Files
USB-RAID-Array/Web/content/ftp/delete.php
T
2026-10-18 15:55:34 +02:00

53 lines
1.6 KiB
PHP

<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin('../../index.php');
requirePermission('delPer', 'You do not have permission to perform this operation.');
require_once __DIR__ . '/../../config/config.php';
$sftp = initializeSFTP();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete'])) {
$itemToDelete = requireRemotePath($_POST['delete'], $defaultPath);
if ($itemToDelete === $defaultPath) {
http_response_code(400);
exit('Cannot delete the workspace root.');
}
$parentPath = remoteDirname($itemToDelete);
if ($parentPath === '/' || $parentPath === '.') {
$parentPath = $defaultPath;
}
if ($sftp->is_dir($itemToDelete)) {
function deleteFolder($sftp, $folderPath) {
$items = $sftp->nlist($folderPath);
if ($items === false) return false;
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$itemPath = $folderPath . '/' . $item;
if ($sftp->is_dir($itemPath)) {
if (!deleteFolder($sftp, $itemPath)) return false;
} else {
if (!$sftp->delete($itemPath)) return false;
}
}
return $sftp->rmdir($folderPath);
}
$success = deleteFolder($sftp, $itemToDelete);
} else {
$success = $sftp->delete($itemToDelete);
}
if (!$success) {
http_response_code(500);
exit('Failed to delete the item.');
}
header("Location: index.php?path=" . urlencode($parentPath));
exit;
}
header("Location: index.php");
exit;