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 를 아파치에 설치해야 한다.
# ftp '접속지 주소' '포트번호'
$ ftp 192.168.1.39
6
Name:
Password:
ftp> help/?
ftp> user
# 로그인 실패시 재 로그인
ftp> ls/!dir
# 서버명령/클라이언트명령 수행
ftp> cd/lcd # 서버/클라이언트
디렉토리 이동.
ftp> delete/rename # 파일 지우기/이름바꾸기
ftp> mkdir/rmdir # 디렉토리 생성/삭제
ftp>
get/put # 파일 전송
ftp> mget/mput
# 여러개 파일 가져올 때
ftp> binary/ascii
# 전송파일타입을 이진/아스키 파일로 지정한다.
ftp> type binary/ascii
# 전송파일타입을 이진/아스키 파일로 지정한다.
ftp> hash # 받는 파일의
양을 화면에 표시 합니다.
ftp> prompt # mget/mput 할 때
y/n 질문을 하지 않게 한다.
ftp> bye/quit # 끝내기
ftp> open/close hostname # 원격 호스트에 접속/단절 한다.
ftp>
status # ascii/binary, glob on/off 등 현재 ftp 세션의 접속 파라미터를
출력한다.
ftp> pwd # 현재의 절대 경로
ftp>
verbose on # 전송중에 발생하는 정보를 화면에 보여준다.
주위: 윈도우 DOS 창에서의 FTP 사용 - 포트번호 지정이 않되고, 파일이 잘 전송이 않된다.
WS-FTP 프로그램 -
포트번호 변경이 안된다.
권장 FTP 편집기 - 울트라에디트
권장 FTP 프로그램 - Cute
FTP, Leech FTP
4. 활용팁
- mget mput명령시에 파일 하나마다 y를 누르는 불편 없애기.
ftp> prompt 하고
ftp> mget 하면 된다.
또는, 접속시에 ftp -i xxx.xxx.xxx.xxx
하면 된다.