|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ronanchilvers\Utility; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Useful operations for files |
|
7
|
|
|
* |
|
8
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class File |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Join a set of paths together |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $separator |
|
16
|
|
|
* @param string $piece... |
|
17
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
3 |
|
static public function join(...$pieces) |
|
20
|
|
|
{ |
|
21
|
3 |
|
$separator = DIRECTORY_SEPARATOR; |
|
22
|
3 |
|
$prefix = ($separator == substr($pieces[0],0,1)) ? $separator : ''; |
|
23
|
3 |
|
foreach ($pieces as &$piece) { |
|
24
|
3 |
|
$piece = trim($piece, $separator); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
3 |
|
return $prefix . implode($separator, $pieces); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Copy files or folders from one location to another |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $source |
|
34
|
|
|
* @param string $dest |
|
35
|
|
|
* @param int $mode The permissions mode for new folders |
|
36
|
|
|
* @return boolean |
|
37
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
static public function cp($source, $dest, $mode = 0750) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!is_readable($source) || (file_exists($dest) && !is_writable($dest))) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
if (!is_dir($source) && !is_file($source)) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
if (is_file($source)) { |
|
48
|
|
|
return copy($source, $dest); |
|
49
|
|
|
} |
|
50
|
|
|
if (!$dir = opendir($source)) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
if (!mkdir($dest, $mode, true)) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
while(false !== ( $file = readdir($dir)) ) { |
|
57
|
|
|
if (($file == '.') || ($file == '..')) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
if (is_dir($source . '/' . $file) ) { |
|
61
|
|
|
if (!static::cp($source . '/' . $file, $dest . '/' . $file)) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
else { |
|
66
|
|
|
if (!copy($source . '/' . $file, $dest . '/' . $file)) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
closedir($dir); |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Remove a file or folder |
|
78
|
|
|
* |
|
79
|
|
|
* In the case of folders, the removal is recursive |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $path |
|
82
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
83
|
|
|
*/ |
|
84
|
|
|
static public function rm($path) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!is_writable($path)) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
if (!is_dir($path) && !is_file($path)) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
if (is_file($path)) { |
|
93
|
|
|
return unlink($path); |
|
94
|
|
|
} |
|
95
|
|
|
if (!$dir = opendir($path)) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
while(false !== ( $file = readdir($dir)) ) { |
|
99
|
|
|
if (($file == '.') || ($file == '..')) { |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
$thisPath = static::join($path, $file); |
|
103
|
|
|
if (is_link($thisPath)) { |
|
104
|
|
|
if (!unlink($thisPath)) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
} else if (is_dir($thisPath) ) { |
|
108
|
|
|
if (!static::rm($thisPath)) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
else { |
|
113
|
|
|
if (!unlink($thisPath)) { |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
closedir($dir); |
|
119
|
|
|
|
|
120
|
|
|
return rmdir($path); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|