<?php
error_reporting
(7);
if
(
$_SERVER
[
'SERVER_ADDR'
])
exit
;
$_GET
[
'exclude'
] =
array
(
'number.txt'
,
'uploads'
,
'Zend'
,
'docs'
,
'cache'
,
'You'
,
'managesdk'
);
$fileobj
=
new
FilerFile();
$path
=
"/data/longtu/"
;
$files
=
$fileobj
->Zip(
$path
);
$path
=
str_replace
(
"/data/longtu/"
,
""
,
$path
);
$config
=
array
(
'hostname'
=>
'xxx.xxx.xx.xxx'
,
'username'
=>
'xxx'
,
'password'
=>
'?xxxxxxxxxxx'
,
'port'
=> 21
);
$ftp
=
new
Ftp();
$ftp
->connect(
$config
);
$LOCAL_ROOT
=
realpath
(dirname(__DIR__).
"/../../"
);
chdir
(
$LOCAL_ROOT
);
foreach
(
$files
as
$k
=>
$v
){
$f
=
$path
.
$v
;
$tmp
=
$ftp
->upload(
$f
,
$f
);
if
(
$tmp
){
echo
"upload $f -> success \n"
;
}
}
class
FilerFile
{
var
$time_path
;
private
$fctimes
=
array
();
function
Zip(
$dir
)
{
$this
->time_path = rtrim(
$dir
,
"/"
).
"/.~~~time.php"
;
$filelist
=
$this
-> GetFileList(
$dir
);
file_put_contents
(
$this
->time_path,
"<?php \n return "
.var_export(
$this
->fctimes,true).
";"
);
return
$filelist
;
}
function
appendFilectime(
$file
)
{
$time_file_path
=
$this
->time_path;
$ftime
= @
include
(
$time_file_path
);
$ftime
=
$ftime
?
$ftime
:
array
();
$time
=
filectime
(
$file
);
if
(!
file_exists
(
$time_file_path
))
file_put_contents
(
$time_file_path
,
"<?php \n"
);
}
function
getFileByFilectime(
$file
)
{
static
$time_data
;
$time_file_path
=
$this
->time_path;
if
(!
$time_data
){
$time_data
= @
include_once
(
$time_file_path
);
}
$time_data
=
$time_data
?
$time_data
:
array
();
if
(
$time_data
[
$file
] ==
filemtime
(
$file
)){
return
false;
}
else
{
return
$file
;
}
}
function
GetFileList(
$dir
,
$path
=
""
)
{
static
$tmpp
=
""
;
if
(
$path
==
""
&& !
$tmpp
){
$tmpp
=
$dir
;
}
$d
= dir(
$dir
);
$files
=
array
();
if
(
$d
)
{
$pathP
=
str_replace
(
$tmpp
,
""
,
$dir
);
$pathP
=
str_replace
(
array
(
"\\\\"
,
"/"
),DIRECTORY_SEPARATOR,
$pathP
);
$pathP
=
str_replace
(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,
$pathP
);
while
(
$f
=
$d
->read())
{
if
(
$f
==
'.'
||
$f
==
'..'
||
$f
{0}==
'.'
||
$f
==
'Zend'
|| in_array(
$f
,
$_GET
[
'exclude'
]))
continue
;
$newdir
= rtrim(
$dir
,
"/"
).
"/"
.
$f
;
if
(
is_dir
(
$newdir
)){
$files
=
array_merge
(
$files
,
$this
->GetFileList(
$newdir
,
$newdir
));
}
else
{
$abspath_file
= (rtrim(
$dir
,
"/"
) ? rtrim(
$dir
,
"/"
).
"/"
:
""
).
$f
;
$this
->fctimes[
$abspath_file
] =
filemtime
(
$abspath_file
);
if
(!
$this
->getFileByFilectime(
$abspath_file
))
continue
;
$file
= (rtrim(
$pathP
,
"/"
) ? rtrim(
$pathP
,
"/"
).
"/"
:
""
).
$f
;
$files
[] =
$file
;
}
}
}
return
$files
;
}
}
class
Ftp {
private
$hostname
=
''
;
private
$username
=
''
;
private
$password
=
''
;
private
$port
= 21;
private
$passive
= TRUE;
private
$debug
= TRUE;
private
$conn_id
= FALSE;
public
function
__construct(
$config
=
array
()) {
if
(
count
(
$config
) > 0) {
$this
->_init(
$config
);
}
}
public
function
connect(
$config
=
array
()) {
if
(
count
(
$config
) > 0) {
$this
->_init(
$config
);
}
if
(FALSE === (
$this
->conn_id = @ftp_connect(
$this
->hostname,
$this
->port))) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_connect"
);
}
return
FALSE;
}
if
( !
$this
->_login()) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_login"
);
}
return
FALSE;
}
if
(
$this
->passive === TRUE) {
ftp_pasv(
$this
->conn_id, TRUE);
}
return
TRUE;
}
public
function
is_dir_exists(
$path
)
{
return
$this
->chgdir(
$path
);
}
public
function
chgdir(
$path
=
''
,
$supress_debug
= FALSE) {
if
(
$path
==
''
OR !
$this
->_isconn()) {
return
FALSE;
}
$result
= @ftp_chdir(
$this
->conn_id,
$path
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE AND
$supress_debug
== FALSE) {
$this
->_error(
"ftp_unable_to_chgdir:dir["
.
$path
.
"]"
);
}
return
FALSE;
}
return
TRUE;
}
public
function
mkdir
(
$path
=
''
,
$permissions
= NULL) {
if
(
$path
==
''
OR !
$this
->_isconn()) {
return
FALSE;
}
$result
= @ftp_mkdir(
$this
->conn_id,
$path
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_mkdir:dir["
.
$path
.
"]"
);
}
return
FALSE;
}
if
( !
is_null
(
$permissions
)) {
$this
->
chmod
(
$path
,(int)
$permissions
);
}
return
TRUE;
}
public
function
upload(
$localpath
,
$remotepath
,
$mode
=
'auto'
,
$permissions
= NULL) {
if
( !
$this
->_isconn()) {
return
FALSE;
}
if
( !
file_exists
(
$localpath
)) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_no_source_file:"
.
$localpath
);
}
return
FALSE;
}
if
(
$mode
==
'auto'
) {
$ext
=
$this
->_getext(
$localpath
);
$mode
=
$this
->_settype(
$ext
);
}
$mode
= (
$mode
==
'ascii'
) ? FTP_ASCII : FTP_BINARY;
$result
= @ftp_put(
$this
->conn_id,
$remotepath
,
$localpath
,
$mode
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_upload:localpath["
.
$localpath
.
"]/remotepath["
.
$remotepath
.
"]"
);
}
return
FALSE;
}
if
( !
is_null
(
$permissions
)) {
$this
->
chmod
(
$remotepath
,(int)
$permissions
);
}
return
TRUE;
}
public
function
download(
$remotepath
,
$localpath
,
$mode
=
'auto'
) {
if
( !
$this
->_isconn()) {
return
FALSE;
}
if
(
$mode
==
'auto'
) {
$ext
=
$this
->_getext(
$remotepath
);
$mode
=
$this
->_settype(
$ext
);
}
$mode
= (
$mode
==
'ascii'
) ? FTP_ASCII : FTP_BINARY;
$result
= @ftp_get(
$this
->conn_id,
$localpath
,
$remotepath
,
$mode
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_download:localpath["
.
$localpath
.
"]-remotepath["
.
$remotepath
.
"]"
);
}
return
FALSE;
}
return
TRUE;
}
public
function
rename(
$oldname
,
$newname
,
$move
= FALSE) {
if
( !
$this
->_isconn()) {
return
FALSE;
}
$result
= @ftp_rename(
$this
->conn_id,
$oldname
,
$newname
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE) {
$msg
= (
$move
== FALSE) ?
"ftp_unable_to_rename"
:
"ftp_unable_to_move"
;
$this
->_error(
$msg
);
}
return
FALSE;
}
return
TRUE;
}
public
function
delete_file(
$file
) {
if
( !
$this
->_isconn()) {
return
FALSE;
}
$result
= @ftp_delete(
$this
->conn_id,
$file
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_delete_file:file["
.
$file
.
"]"
);
}
return
FALSE;
}
return
TRUE;
}
public
function
delete_dir(
$path
) {
if
( !
$this
->_isconn()) {
return
FALSE;
}
$path
= preg_replace(
"/(.+?)\/*$/"
,
"\\1/"
,
$path
);
$filelist
=
$this
->filelist(
$path
);
if
(
$filelist
!== FALSE AND
count
(
$filelist
) > 0) {
foreach
(
$filelist
as
$item
) {
if
( ! @delete_file(
$item
)) {
$this
->delete_dir(
$item
);
}
}
}
$result
= @ftp_rmdir(
$this
->conn_id,
$path
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_delete_dir:dir["
.
$path
.
"]"
);
}
return
FALSE;
}
return
TRUE;
}
/**
* 修改文件权限
*
* @access
public
* @param string 目录标识(ftp)
* @
return
boolean
*/
public
function
chmod
(
$path
,
$perm
) {
if
( !
$this
->_isconn()) {
return
FALSE;
}
if
( ! function_exists(
'ftp_chmod'
)) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_chmod(function)"
);
}
return
FALSE;
}
$result
= @ftp_chmod(
$this
->conn_id,
$perm
,
$path
);
if
(
$result
=== FALSE) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_unable_to_chmod:path["
.
$path
.
"]-chmod["
.
$perm
.
"]"
);
}
return
FALSE;
}
return
TRUE;
}
public
function
filelist(
$path
=
'.'
) {
if
( !
$this
->_isconn()) {
return
FALSE;
}
return
ftp_nlist(
$this
->conn_id,
$path
);
}
public
function
close() {
if
( !
$this
->_isconn()) {
return
FALSE;
}
return
@ftp_close(
$this
->conn_id);
}
private
function
_init(
$config
=
array
()) {
foreach
(
$config
as
$key
=>
$val
) {
if
(isset(
$this
->
$key
)) {
$this
->
$key
=
$val
;
}
}
$this
->hostname = preg_replace(
'|.+?://|'
,
''
,
$this
->hostname);
}
private
function
_login() {
return
@ftp_login(
$this
->conn_id,
$this
->username,
$this
->password);
}
private
function
_isconn() {
if
( !
is_resource
(
$this
->conn_id)) {
if
(
$this
->debug === TRUE) {
$this
->_error(
"ftp_no_connection"
);
}
return
FALSE;
}
return
TRUE;
}
private
function
_getext(
$filename
) {
if
(FALSE ===
strpos
(
$filename
,
'.'
)) {
return
'txt'
;
}
$extarr
=
explode
(
'.'
,
$filename
);
return
end
(
$extarr
);
}
private
function
_settype(
$ext
) {
$text_type
=
array
(
'txt'
,
'text'
,
'php'
,
'phps'
,
'php4'
,
'js'
,
'css'
,
'htm'
,
'html'
,
'phtml'
,
'shtml'
,
'log'
,
'xml'
);
return
(in_array(
$ext
,
$text_type
)) ?
'ascii'
:
'binary'
;
}
private
function
_error(
$msg
) {
return
@
file_put_contents
(
'/tmp/ftp_err.log'
,
"date["
.
date
(
"Y-m-d H:i:s"
).
"]-hostname["
.
$this
->hostname.
"]-username["
.
$this
->username.
"]-password["
.
$this
->password.
"]-msg["
.
$msg
.
"]\n"
, FILE_APPEND);
}
}