-
Notifications
You must be signed in to change notification settings - Fork 11
/
function.ftp_rmdirr.php
81 lines (66 loc) · 2.14 KB
/
function.ftp_rmdirr.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
<?php
/**
* Recursively delete the files in a directory via FTP.
*
* @author Aidan Lister <[email protected]>
* @version 1.0.0
* @link http://aidanlister.com/2004/04/recursively-deleting-directories-via-ftp/
* @param resource $ftp_stream The link identifier of the FTP connection
* @param string $directory The directory to delete
*/
function ftp_rmdirr($ftp_stream, $directory)
{
// Sanity check
if (!is_resource($ftp_stream) ||
get_resource_type($ftp_stream) !== 'FTP Buffer') {
return false;
}
// Init
$i = 0;
$files = array();
$folders = array();
$statusnext = false;
$currentfolder = $directory;
// Get raw file listing
$list = ftp_rawlist($ftp_stream, $directory, true);
// Iterate listing
foreach ($list as $current) {
// An empty element means the next element will be the new folder
if (empty($current)) {
$statusnext = true;
continue;
}
// Save the current folder
if ($statusnext === true) {
$currentfolder = substr($current, 0, -1);
$statusnext = false;
continue;
}
// Split the data into chunks
$split = preg_split('[ ]', $current, 9, PREG_SPLIT_NO_EMPTY);
$entry = $split[8];
$isdir = ($split[0]{0} === 'd') ? true : false;
// Skip pointers
if ($entry === '.' || $entry === '..') {
continue;
}
// Build the file and folder list
if ($isdir === true) {
$folders[] = $currentfolder . '/' . $entry;
} else {
$files[] = $currentfolder . '/' . $entry;
}
}
// Delete all the files
foreach ($files as $file) {
ftp_delete($ftp_stream, $file);
}
// Delete all the directories
// Reverse sort the folders so the deepest directories are unset first
rsort($folders);
foreach ($folders as $folder) {
ftp_rmdir($ftp_stream, $folder);
}
// Delete the final folder and return its status
return ftp_rmdir($ftp_stream, $directory);
}