Sanırım şimdi olur.
<?php
use phpseclib3\Net\SFTP;
class ogcp_ssh2 {
private $sftp;
private $error;
private $host;
private $port;
private $username;
private $password;
private $debug = true;
private function logError($message, $function = '') {
$timestamp = date('Y-m-d H:i:s');
$logMessage = "[{$timestamp}] {$function}: {$message}\n";
if($this->debug) {
error_log($logMessage, 3, 'sftp_errors.log');
$this->error = $message;
}
}
public function __construct() {
$this->sftp = null;
$this->error = "";
}
public function ConnectwAuth($host, $port = 22, $user, $pass) {
if ($host == "" || $user == "" || $pass == "") {
$this->logError("Eksik parametreler", __FUNCTION__);
return false;
}
try {
$this->host = gethostbyname($host);
$this->port = $port;
$this->username = $user;
$this->password = $pass;
$this->sftp = new SFTP($this->host, $this->port);
if ($this->sftp->login($user, $pass)) {
$this->logError("Bağlantı başarılı", __FUNCTION__);
return true;
} else {
$this->logError("Bağlantı başarısız", __FUNCTION__);
return false;
}
} catch (\Exception $e) {
$this->logError("Bağlantı hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function SFTP_FileLink($filepath) {
try {
if (!$this->sftp) {
$this->logError("SFTP bağlantısı yok", __FUNCTION__);
return false;
}
if (!$this->sftp->file_exists($filepath)) {
$this->logError("Dosya bulunamadı: " . $filepath, __FUNCTION__);
return false;
}
return $filepath;
} catch (\Exception $e) {
$this->logError("FileLink hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function SFTP_DownloadFile($remote, $local) {
try {
if (!$this->sftp) {
$this->logError("SFTP bağlantısı yok", __FUNCTION__);
return false;
}
if (!$this->sftp->file_exists($remote)) {
$this->logError("Uzak dosya bulunamadı: " . $remote, __FUNCTION__);
return false;
}
if ($this->sftp->get($remote, $local)) {
$this->logError("Dosya indirildi: " . $remote, __FUNCTION__);
return true;
} else {
$this->logError("Dosya indirilemedi: " . $remote, __FUNCTION__);
return false;
}
} catch (\Exception $e) {
$this->logError("Download hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function SFTP_UploadFile($local, $remote) {
try {
if (!$this->sftp) {
$this->logError("SFTP bağlantısı yok", __FUNCTION__);
return false;
}
if (!file_exists($local)) {
$this->logError("Yerel dosya bulunamadı: " . $local, __FUNCTION__);
return false;
}
if ($this->sftp->put($remote, $local, SFTP::SOURCE_LOCAL_FILE)) {
$this->logError("Dosya yüklendi: " . $remote, __FUNCTION__);
return true;
} else {
$this->logError("Dosya yüklenemedi: " . $remote, __FUNCTION__);
return false;
}
} catch (\Exception $e) {
$this->logError("Upload hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function Disconnect() {
if ($this->sftp) {
$this->sftp = null;
}
return true;
}
public function getLastError() {
return $this->error;
}
}
?>