글
Dev/php 2010/01/25 16:19ssh2_connect 함수를 사용해 원격서버에 파일올리기
<?
# 설정
$host = 222.222.222.222";
$connect_id = "ididid";
$connect_pw = "pwpwpw";
class SFTPConnection
{
private $connection;
private $sftp;
public function __construct($host, $port=22)
{
$this->connection = @ssh2_connect($host, $port);
if (! $this->connection)
throw new Exception("Could not connect to $host on port $port.");
}
public function login($username, $password)
{
if (! @ssh2_auth_password($this->connection, $username, $password))
throw new Exception("Could not authenticate with username $username " .
"and password $password.");
$this->sftp = @ssh2_sftp($this->connection);
if (! $this->sftp)
throw new Exception("Could not initialize SFTP subsystem.");
}
public function uploadFile($local_file, $remote_file)
{
$sftp = $this->sftp;
$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
if (! $stream)
throw new Exception("Could not open file: $remote_file");
$data_to_send = @file_get_contents($local_file);
if ($data_to_send === false)
throw new Exception("Could not open local file: $local_file.");
if (@fwrite($stream, $data_to_send) === false)
throw new Exception("Could not send data from file: $local_file.");
@fclose($stream);
}
}
$sftp = new SFTPConnection($host, 22);
$sftp->login($connect_id, $connect_pw);
//$sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received");
$sftp->uploadFile($tmp_file , $randumid);
?>
원격지 서버의 ftp 프로그램이 secure ftp(22번포트사용) 일 경우에는 ftp_connect () 함수를 이용할 수 없다.
따라서 ssh2_connect() 함수를 이용해야 하는데 이 함수를 사용하기 위해서는 openssl(lib), libssh2, php_ssh2 를 아파치에 설치해야 한다.
# 설정
$host = 222.222.222.222";
$connect_id = "ididid";
$connect_pw = "pwpwpw";
class SFTPConnection
{
private $connection;
private $sftp;
public function __construct($host, $port=22)
{
$this->connection = @ssh2_connect($host, $port);
if (! $this->connection)
throw new Exception("Could not connect to $host on port $port.");
}
public function login($username, $password)
{
if (! @ssh2_auth_password($this->connection, $username, $password))
throw new Exception("Could not authenticate with username $username " .
"and password $password.");
$this->sftp = @ssh2_sftp($this->connection);
if (! $this->sftp)
throw new Exception("Could not initialize SFTP subsystem.");
}
public function uploadFile($local_file, $remote_file)
{
$sftp = $this->sftp;
$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
if (! $stream)
throw new Exception("Could not open file: $remote_file");
$data_to_send = @file_get_contents($local_file);
if ($data_to_send === false)
throw new Exception("Could not open local file: $local_file.");
if (@fwrite($stream, $data_to_send) === false)
throw new Exception("Could not send data from file: $local_file.");
@fclose($stream);
}
}
$sftp = new SFTPConnection($host, 22);
$sftp->login($connect_id, $connect_pw);
//$sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received");
$sftp->uploadFile($tmp_file , $randumid);
?>
원격지 서버의 ftp 프로그램이 secure ftp(22번포트사용) 일 경우에는 ftp_connect () 함수를 이용할 수 없다.
따라서 ssh2_connect() 함수를 이용해야 하는데 이 함수를 사용하기 위해서는 openssl(lib), libssh2, php_ssh2 를 아파치에 설치해야 한다.
RECENT COMMENT