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

41 lines
1.3 KiB
PHP

<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin('../../index.php');
requirePermission('upPer', 'You do not have permission to perform this operation.');
require_once __DIR__ . '/../../config/config.php';
$sftp = initializeSFTP();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$oldPath = isset($_POST['path']) ? $_POST['path'] : '';
$newName = isset($_POST['newName']) ? trim($_POST['newName']) : '';
if (empty($oldPath) || empty($newName)) {
die("Missing required information for renaming.");
}
$oldPath = requireRemotePath($oldPath, $defaultPath);
if ($oldPath === $defaultPath || !isRemoteNameValid($newName)) {
http_response_code(400);
exit('Invalid item name or workspace root.');
}
$directoryPath = remoteDirname($oldPath);
$newPath = rtrim($directoryPath, '/') . '/' . $newName;
if ($sftp->file_exists($newPath) || $sftp->is_dir($newPath)) {
die("Error: A file or directory with this name already exists.");
}
if ($sftp->rename($oldPath, $newPath)) {
header("Location: index.php?path=" . urlencode($directoryPath));
exit;
} else {
die("Failed to rename the item. Please try again.");
}
} else {
header("Location: index.php");
exit;
}
?>