🐘
SFTPtrash.php
Back
📝 Php ⚡ Executable Ctrl+S: Save • Ctrl+R: Run • Ctrl+F: Find
<?php /** * SFTPtrash.php * Move normal files/folders into _wastebasket, * but permanently delete _wastebasket itself (recursive). */ declare(strict_types=1); header('Content-Type: application/json; charset=utf-8'); ob_start(); session_start(); $response = ['success' => false, 'message' => '']; try { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('POST only'); } if (empty($_SESSION['sftp_connected']) || empty($_SESSION['sftp_config'])) { throw new Exception('Not connected'); } require_once __DIR__ . '/SFTPconnector.php'; $config = $_SESSION['sftp_config']; $input = json_decode(file_get_contents('php://input'), true); $targetPath = $input['path'] ?? ''; if (!$targetPath) throw new Exception('No file path provided'); $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("Reconnection failed: $result"); $filename = basename($targetPath); $dirPath = rtrim(dirname($targetPath), '/'); if ($dirPath === '' || $dirPath === '.') $dirPath = '/'; // === Permanent delete if _wastebasket === if ($filename === '_wastebasket') { if (!recursiveDeleteSafe($connector, $targetPath)) { throw new Exception("Failed to permanently delete _wastebasket"); } $response['success'] = true; $response['message'] = "_wastebasket deleted permanently."; } else { // === Normal move to trash === $trashDir = $dirPath . '/_wastebasket'; $existing = $connector->listDirectory($dirPath); $trashExists = false; if (is_array($existing)) { foreach ($existing as $f) { if (!empty($f['is_dir']) && $f['name'] === '_wastebasket') { $trashExists = true; break; } } } if (!$trashExists) { $mk = $connector->createDirectory($trashDir); if ($mk !== true) throw new Exception("Cannot create $trashDir"); } $tmp = tempnam(sys_get_temp_dir(), 'sftp_'); $dl = $connector->downloadFile($targetPath, $tmp); if ($dl !== true) { unlink($tmp); throw new Exception("Download failed for $targetPath"); } $newPath = $trashDir . '/' . $filename; $up = $connector->uploadFile($tmp, $newPath); unlink($tmp); if ($up !== true) throw new Exception("Upload failed to $newPath"); $connector->deleteFile($targetPath); $response['success'] = true; $response['message'] = "Moved '$filename' to _wastebasket."; } } catch (Throwable $e) { $response['success'] = false; $response['message'] = $e->getMessage(); } if (ob_get_length()) ob_clean(); echo json_encode($response, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); exit; /** * Recursive delete that works across all SFTP servers. */ function recursiveDeleteSafe($connector, string $path): bool { $items = $connector->listDirectory($path); if (is_array($items)) { foreach ($items as $item) { if ($item['name'] === '.' || $item['name'] === '..') continue; $childPath = rtrim($path, '/') . '/' . $item['name']; if (!empty($item['is_dir'])) { recursiveDeleteSafe($connector, $childPath); } else { $connector->deleteFile($childPath); } } } // delete the folder itself last return $connector->deleteFile($path) === true; }