|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Wrapper for php file commmands |
|
4
|
|
|
* |
|
5
|
|
|
* @package erdiko/core |
|
6
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
|
7
|
|
|
* @author Varun Brahme |
|
8
|
|
|
* @author Coleman Tung |
|
9
|
|
|
* @author John Arroyo <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace erdiko\core\datasource; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class File |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Default Path |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $_filePath = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Contructor |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $defaultPath |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($defaultPath = null) |
|
27
|
|
|
{ |
|
28
|
|
|
if (isset($defaultPath)) { |
|
29
|
|
|
$this->_filePath=$defaultPath; |
|
30
|
|
|
} else { |
|
31
|
|
|
$rootFolder=dirname(dirname(dirname(__DIR__))); |
|
32
|
|
|
$this->_filePath=$rootFolder."/var"; |
|
33
|
|
|
} |
|
34
|
|
|
if (!is_dir($this->_filePath)) { |
|
35
|
|
|
mkdir($this->_filePath, 0775, true); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create Directory |
|
41
|
|
|
* If path doesn't exist, create it |
|
42
|
|
|
*/ |
|
43
|
|
|
private function createDir($path) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!is_dir($path)) { |
|
46
|
|
|
$success = mkdir($path, 0775, true); |
|
47
|
|
|
if (!$success) { |
|
48
|
|
|
throw new \Exception("Cannot create folder {$path}. Check file system permissions."); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Write string to file |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $content |
|
57
|
|
|
* @param string $filename |
|
58
|
|
|
* @param string $pathToFile |
|
59
|
|
|
* @param string $mode - Default mode: w |
|
60
|
|
|
* @return int - bytes written to file |
|
61
|
|
|
*/ |
|
62
|
|
|
public function write($content, $filename, $pathToFile = null, $mode = "w") |
|
63
|
|
|
{ |
|
64
|
|
|
if ($pathToFile == null) { |
|
|
|
|
|
|
65
|
|
|
$pathToFile = $this->_filePath; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->createDir($pathToFile); |
|
69
|
|
|
|
|
70
|
|
|
$fileHandle = fopen($pathToFile."/".$filename, $mode); |
|
71
|
|
|
$success = fwrite($fileHandle, $content); |
|
72
|
|
|
fclose($fileHandle); |
|
73
|
|
|
|
|
74
|
|
|
return $success; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Read string to file |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $filename |
|
81
|
|
|
* @param string $pathToFile |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
View Code Duplication |
public function read($filename, $pathToFile = null) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
if(!$this->fileExists($filename, $pathToFile)) |
|
87
|
|
|
throw new \Exception("File, '{$filename}', does not exist."); |
|
88
|
|
|
|
|
89
|
|
|
if ($pathToFile==null) { |
|
|
|
|
|
|
90
|
|
|
return file_get_contents($this->_filePath."/".$filename); |
|
91
|
|
|
} else { |
|
92
|
|
|
return file_get_contents($pathToFile."/".$filename); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Delete a file |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $filename |
|
100
|
|
|
* @param string $pathToFile |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
public function delete($filename, $pathToFile = null) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
if ($pathToFile==null) { |
|
|
|
|
|
|
106
|
|
|
$pathToFile=$this->_filePath; |
|
107
|
|
|
} |
|
108
|
|
|
if (file_exists($pathToFile."/".$filename)) { |
|
109
|
|
|
return unlink($pathToFile."/".$filename); |
|
110
|
|
|
} else { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Move a file |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $filename |
|
119
|
|
|
* @param string $pathTo |
|
120
|
|
|
* @param string $pathToFrom |
|
|
|
|
|
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public function move($filename, $pathTo, $pathFrom = null) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
if ($pathFrom==null) { |
|
126
|
|
|
$pathFrom=$this->_filePath; |
|
127
|
|
|
} |
|
128
|
|
|
if (file_exists($pathFrom."/".$filename)) { |
|
129
|
|
|
$this->createDir($pathTo); |
|
130
|
|
|
return rename($pathFrom."/".$filename, $pathTo."/".$filename); |
|
131
|
|
|
} else { |
|
132
|
|
|
return null; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Rename a file |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $oldName |
|
140
|
|
|
* @param string $pathTo |
|
|
|
|
|
|
141
|
|
|
* @param string $pathToFrom |
|
|
|
|
|
|
142
|
|
|
* @return bool |
|
143
|
|
|
* @todo consider merging rename() and move() into one method |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
public function rename($oldName, $newName, $pathToFile = null) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
if ($pathToFile==null) { |
|
148
|
|
|
$pathToFile=$this->_filePath; |
|
149
|
|
|
} |
|
150
|
|
|
if (file_exists($pathToFile."/".$oldName)) { |
|
151
|
|
|
$this->createDir($pathToFile); |
|
152
|
|
|
return rename($pathToFile."/".$oldName, $pathToFile."/".$newName); |
|
153
|
|
|
} else { |
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Copy a file |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $filename |
|
162
|
|
|
* @param string $newFilePath |
|
163
|
|
|
* @param string $newFileName |
|
164
|
|
|
* @param string $pathToFile |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function copy($filename, $newFilePath, $newFileName = null, $pathToFile = null) |
|
168
|
|
|
{ |
|
169
|
|
|
if ($pathToFile==null) { |
|
|
|
|
|
|
170
|
|
|
$pathToFile=$this->_filePath; |
|
171
|
|
|
} |
|
172
|
|
|
if ($newFileName==null) { |
|
|
|
|
|
|
173
|
|
|
$newFileName=$filename; |
|
174
|
|
|
} |
|
175
|
|
|
if (file_exists($pathToFile."/".$filename)) { |
|
176
|
|
|
return copy($pathToFile."/".$filename, $newFilePath."/".$newFileName); |
|
177
|
|
|
} else { |
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Check if a file exists |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $filename |
|
186
|
|
|
* @param string $pathToFile |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
|
public function fileExists($filename, $pathToFile = null) |
|
190
|
|
|
{ |
|
191
|
|
|
if ($pathToFile==null) { |
|
|
|
|
|
|
192
|
|
|
$pathToFile=$this->_filePath; |
|
193
|
|
|
} |
|
194
|
|
|
return file_exists($pathToFile."/".$filename); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Destructor |
|
199
|
|
|
*/ |
|
200
|
|
|
public function __destruct() |
|
201
|
|
|
{ |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|