-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp_sync.php
146 lines (118 loc) · 2.91 KB
/
ftp_sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
require_once('ftp.php');
/**
* sync local directory to ftp remote directory
*/
class ftp_sync
{
public static $ftp_host = null;
public static $ftp_user = null;
public static $ftp_pass = null;
public static $ftp_port = null;
public static $ftp_path = null;
public static $directory = null;
public static $debug = false;
public static $errors = [];
public static $status = true;
private static $master_pwd = null;
private static function set_error($_msg)
{
self::$status = false;
self::$errors[] = $_msg;
}
public static function error()
{
return self::$errors;
}
public static function run()
{
if(self::$debug)
{
ini_set('display_startup_errors', 'On');
ini_set('error_reporting', 'E_ALL | E_STRICT');
ini_set('track_errors', 'On');
ini_set('display_errors', 1);
error_reporting(E_ALL);
}
if(!self::$ftp_host) self::set_error("ftp_host not set");
if(!self::$ftp_user) self::set_error("ftp_user not set");
if(!self::$ftp_pass) self::set_error("ftp_pass not set");
if(!self::$ftp_port) self::set_error("ftp_port not set");
if(!self::$ftp_path) self::set_error("ftp_path not set");
if(!self::$directory) self::set_error("directory not set");
if(!self::$status) return false;
$connect = ftp::connect(self::$ftp_host, self::$ftp_user, self::$ftp_pass, self::$ftp_port);
if(!$connect)
{
return self::set_error('can not connect to server');
}
if(!@ftp::chdir(self::$ftp_path))
{
return self::set_error('can not chdir to remote server');
}
self::$master_pwd = ftp::pwd();
if(!@chdir(self::$directory))
{
return self::set_error('can not chdir to ftp path');
}
if(!self::$status) return false;
self::clean(self::$master_pwd);
ftp::chdir(self::$master_pwd);
self::send(self::$directory);
ftp::close();
return true;
}
private static function clean($_directory)
{
$list = ftp::nlist($_directory);
if(is_array($list))
{
foreach ($list as $key => $value)
{
if(@ftp::chdir($value))
{
self::clean($value);
ftp::rmdir($value);
}
else
{
ftp::delete($value);
}
}
}
}
private static function send($_directory)
{
chdir($_directory);
$list = glob(getcwd(). DIRECTORY_SEPARATOR. "*");
if(is_array($list))
{
foreach ($list as $key => $value)
{
if(is_dir($value))
{
$folder = explode(DIRECTORY_SEPARATOR, $value);
$folder = end($folder);
$new_dir = ftp::mkdir($folder);
if($new_dir)
{
ftp::chdir($new_dir);
}
self::send($value);
$current_pwd = ftp::pwd();
$folder = explode(DIRECTORY_SEPARATOR, $current_pwd);
array_pop($folder);
$folder = implode(DIRECTORY_SEPARATOR, $folder);
ftp::chdir($folder);
}
else
{
$file = explode(DIRECTORY_SEPARATOR, $value);
$file = end($file);
ftp::put(ftp::pwd(). DIRECTORY_SEPARATOR. $file, $value, FTP_ASCII);
}
}
}
}
}
?>