-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_all_files.php
136 lines (127 loc) · 4.13 KB
/
ftp_all_files.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
<?php
// https://stackoverflow.com/questions/49250700/ftp-rawlist-doesnt-list-htaccess
function ftp_all_files($resource, $directory = ".") {
$rawlist = ftp_rawlist($resource, "-a " . $directory);
$rawlist = array_values(array_filter($rawlist, function ($item) { return !preg_match('/\.?\.$/', $item); }));
$prettylist = array_map(function ($item) use ($directory) { return ftp_raw2pretty($directory, $item); }, $rawlist);
foreach ($prettylist as &$item) {
if ($item["type"] == "dir") {
$item["children"] = ftp_all_files($resource, $item["path"]);
}
}
return $prettylist;
}
// https://www.garron.me/en/go2linux/ls-file-permissions.html
function ftp_raw2pretty($parent_dir, $rawlistItem) {
$cols = preg_split("/\s+/", $rawlistItem);
$parent_dir = $parent_dir == "/" ? "" : $parent_dir;
$pretty = [
"name" => $cols[8],
"path" => str_replace("-a ", "", $parent_dir ."/". $cols[8]),
"parent" => str_replace("-a ", "", $parent_dir),
"type" => "file",
"size" => $cols[4], // in bytes
"last-mod" => implode(" ", [$cols[5], $cols[6], $cols[7]]),
"perm" => $cols[0],
"user" => $cols[2],
"group" => $cols[3],
"links" => $cols[1],
];
if ($cols[0][0] == "d") {
$pretty["type"] = "dir";
} else if ($cols[0][0] == "l") {
$pretty["type"] = "link";
}
return $pretty;
}
// expects pretty file / filelist
function ftp_delete_files($conn, $files, &$fails = null) {
if (!$fails) {
$fails = [];
}
// array of files
if ( is_array( array_values($files)[0] ) ) {
foreach ($files as $file) {
ftp_delete_files($conn, $file, $fails);
}
} else {
// just file
$file = $files;
// it's a folder
if ($file["type"] == "dir") {
// non-empty folder
if ( isset($file["children"]) ) {
// first delete all child files (and folders)
foreach ($file["children"] as $child) {
ftp_delete_files($conn, $child, $fails);
}
// then delete the (parent) folder
if (!@ftp_rmdir($conn, $file["path"])) {
$fails[] = $file;
}
} else {
// empty folder
if (!@ftp_rmdir($conn, $file["path"])) {
$fails[] = $file;
}
}
} else {
// it's a file
if (!@ftp_delete($conn, $file["path"])) {
$fails[] = $file;
}
}
}
// for whatever reasons, these could not be deleted
if (count($fails) > 0) {
return $fails;
}
return true;
}
/* example ftp_all_files() output
Array
(
[0] => Array
(
[name] => cgi-bin
[path] => public_html/cgi-bin
[parent] => public_html
[type] => dir
[size] => 4096
[last-mod] => Mar 13 11:50
[perm] => drwxr-xr-x
[user] => 769
[group] => test
[links] => 2
[children] => Array
(
[0] => Array
(
[name] => .htaccess
[path] => public_html/cgi-bin/.htaccess
[parent] => public_html/cgi-bin
[type] => file
[size] => 11
[last-mod] => Mar 13 11:44
[perm] => -rw-r--r--
[user] => 769
[group] => test
[links] => 1
)
)
)
[1] => Array
(
[name] => index.html
[path] => public_html/index.html
[parent] => public_html
[type] => file
[size] => 608
[last-mod] => Mar 13 10:49
[perm] => -rw-r--r--
[user] => 769
[group] => test
[links] => 1
)
)
*/