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

48 lines
1.4 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' && isset($_POST['action']) && $_POST['action'] === 'createFile') {
$currentPath = requireRemotePath($_POST['currentPath'] ?? $defaultPath, $defaultPath);
if (!isset($_POST['fileName'])) {
http_response_code(400);
echo "File name is required";
exit;
}
$fileName = $_POST['fileName'];
if (strpos($fileName, '.') === false) {
http_response_code(400);
echo "File name must include an extension (e.g., .txt, .html, .php)";
exit;
}
$fileName = preg_replace('/[^\w\-\.]/', '_', $fileName);
if (!isRemoteNameValid($fileName)) {
http_response_code(400);
exit('Invalid file name.');
}
$newFilePath = rtrim($currentPath, '/') . '/' . $fileName;
header('Content-Type: application/json; charset=UTF-8');
if ($sftp->put($newFilePath, '')) {
echo json_encode(['success' => true, 'filePath' => $newFilePath]);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Failed to create file.']);
}
exit;
}
http_response_code(400);
echo "Invalid request";
exit;