🐘
newfile.php
Back
📝 Php ⚡ Executable Ctrl+S: Save • Ctrl+R: Run • Ctrl+F: Find
<?php // /core/php/sftp/newfile.php declare(strict_types=1); header('Content-Type: application/json; charset=utf-8'); session_set_cookie_params([ 'path' => '/', 'secure' => true, 'httponly' => true, 'samesite' => 'None' ]); session_start(); // Ensure clean output ob_start(); $response = ['success' => false, 'message' => '']; // --- 1. Session check --- if (empty($_SESSION['sftp_connected']) || empty($_SESSION['sftp_config'])) { $response['message'] = 'Not connected to SFTP'; echo json_encode($response); exit; } // --- 2. Load dependencies --- require_once __DIR__ . '/SFTPconnector.php'; $config = $_SESSION['sftp_config']; // --- 3. Parse request --- $raw = file_get_contents('php://input'); $input = json_decode($raw, true); $path = $input['path'] ?? ''; $content = $input['content'] ?? ''; if (!$path) { $response['message'] = 'No file path provided'; echo json_encode($response); exit; } // --- 4. Connect using session credentials --- try { $connector = extension_loaded('ssh2') ? new SFTPConnector() : new SystemSFTPConnector(); $result = $connector->connect($config['host'], (int)$config['port'], $config['username'], $config['password']); if ($result !== true) { throw new Exception('Connection failed: ' . $result); } // --- 5. Create file --- $tmp = tempnam(sys_get_temp_dir(), 'sftp_'); file_put_contents($tmp, $content); $upload = $connector->uploadFile($tmp, $path); unlink($tmp); if ($upload === true) { $response = [ 'success' => true, 'message' => 'File created successfully', 'path' => $path ]; } else { throw new Exception(is_string($upload) ? $upload : 'Write failed'); } } catch (Throwable $e) { $response['message'] = $e->getMessage(); } // --- 6. Output clean JSON --- ob_clean(); echo json_encode($response, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); exit;