<?php
class
ftp{
private
$config
=NULL ;
private
$conn
= NULL;
public
function
init(
$config
){
$this
->config =
$config
;
}
public
function
connect(){
return
$this
->conn = ftp_connect(
$this
->config[
'host'
],
$this
->config[
'port'
]));
}
public
function
download(
$remote
,
$local
,
$mode
=
'auto'
){
return
$result
= @ftp_get(
$this
->conn,
$localpath
,
$remotepath
,
$mode
);
}
public
function
upload(
$remote
,
$local
,
$mode
=
'auto'
){
return
$result
= @ftp_put(
$this
->conn,
$localpath
,
$remotepath
,
$mode
);
}
public
function
remove(
$remote
){
return
$result
= @ftp_delete(
$this
->conn_id,
$file
);
}
}
$config
=
array
(
'hostname'
=>
'localhost'
,
'username'
=>
'root'
,
'password'
=>
'root'
,
'port'
=> 21
) ;
$ftp
=
new
Ftp();
$ftp
->connect(
$config
);
$ftp
->upload(
'ftp_err.log'
,
'ftp_upload.log'
);
$ftp
->download(
'ftp_upload.log'
,
'ftp_download.log'
);
class
sftp{
private
$config
=NULL ;
private
$conn
= NULL;
private
$use_pubkey_file
= false;
public
function
init(
$config
){
$this
->config =
$config
;
}
public
function
connect(){
$methods
[
'hostkey'
] =
$use_pubkey_file
?
'ssh-rsa'
: [] ;
$con
= ssh2_connect(
$this
->config[
'host'
],
$this
->config[
'port'
],
$methods
);
if
(
$use_pubkey_file
){
$rc
= ssh2_auth_pubkey_file(
$conn
,
$this
->config[
'user'
],
$this
->config[
'pubkey_file'
],
$this
->config[
'privkey_file'
],
$this
->config[
'passphrase'
])
);
}
else
{
$rc
= ssh2_auth_password(
$conn
,
$this
->conf_[
'user'
],
$this
->conf_[
'passwd'
]);
}
return
$rc
;
}
public
function
download(
$remote
,
$local
){
return
ssh2_scp_recv(
$this
->conn_,
$remote
,
$local
);
}
public
function
upload(
$remote
,
$local
,
$file_mode
=0664){
return
ssh2_scp_send(
$this
->conn_,
$local
,
$remote
,
$file_mode
);
}
public
function
remove(
$remote
){
$sftp
= ssh2_sftp(
$this
->conn_);
$rc
= false;
if
(
is_dir
(
"ssh2.sftp://{$sftp}/{$remote}"
)) {
$rc
= false ;
$rc
= ssh2_sftp_rmdir(
$sftp
,
$remote
);
}
else
{
$rc
= ssh2_sftp_unlink(
$sftp
,
$remote
);
}
return
$rc
;
}
}
$config
= [
"host"
=>
"192.168.1.1 "
,
"user"
=>
"***"
,
"port"
=>
"22"
,
"pubkey_path"
=>
"/root/.ssh/id_rsa.pub"
,
"privkey_path"
=>
"/root/.ssh/id_rsa"
,
];
$handle
=
new
SftpAccess();
$handle
->init(
$config
);
$rc
=
$handle
->connect();
$handle
->getData(remote,
$local
);