Witam,
Mam taki kod w php który łączy mi się po ssh do serwer i odczytuje plik znajdujący się na serwerze
i mam pytanie jak zrobić żeby np dopisać dane do tego pliku albo wykasować całe dane i nadpisać nowymi bez ruszania pliku na serwerze
<?php
$host = "namehost";
$port = 22;
$username = "username";
$password = "password";
$connection = NULL;
$remote_file_path = "/home/user/test.txt";
try {
// Sprawdzanie połączenia z serwerem
$connection = ssh2_connect($host, $port);
if(!$connection){
throw new \Exception("Could not connect to $host on port $port");
}
// Sprawdzanie poprawnych danych logowania
$auth = ssh2_auth_password($connection, $username, $password);
if(!$auth){
throw new \Exception("Could not authenticate with username $username and password ");
}
$sftp = ssh2_sftp($connection);
if(!$sftp){
throw new \Exception("Could not initialize SFTP subsystem.");
}
$stream = fopen("ssh2.sftp://".$sftp.$remote_file_path, 'r');
if (! $stream) {
throw new \Exception("Could not open file: ");
}
$contents = stream_get_contents($stream);
echo "<pre>"; print_r($contents); echo "</pre>";
@fclose($stream);
$connection = NULL;
} catch (Exception $e) {
echo "Error due to :".$e->getMessage();
}
?>