<?php
// /core/php/sftp/newfile.php
// Creates a new file on the SFTP server
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Start session to access SFTP connection
session_start();
// Set JSON header
header('Content-Type: application/json');
try {
// Get the raw POST data
$input = file_get_contents('php://input');
$data = json_decode($input, true);
// Log for debugging
error_log("newfile.php - Received data: " . print_r($data, true));
error_log("newfile.php - Session exists: " . (isset($_SESSION['sftp']) ? 'yes' : 'no'));
// Validate input
if (!isset($data['path'])) {
throw new Exception('Path is required');
}
$path = $data['path'];
$content = isset($data['content']) ? $data['content'] : '';
// Check if SFTP connection exists in session
if (!isset($_SESSION['sftp']) || !$_SESSION['sftp']) {
throw new Exception('No active SFTP connection. Please connect first.');
}
$sftp = $_SESSION['sftp'];
// Create the file
$result = $sftp->put($path, $content);
if ($result === false) {
throw new Exception('Failed to create file on SFTP server');
}
// Success
echo json_encode([
'success' => true,
'message' => 'File created successfully',
'path' => $path
]);
} catch (Exception $e) {
error_log("newfile.php - Error: " . $e->getMessage());
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}
?>