-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.php
261 lines (222 loc) · 6.69 KB
/
File.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace WebStream\IO;
use WebStream\Exception\Extend\IOException;
/**
* File
* 状態を表さないもの(ファイルパス等)はキャッシュし
* 状態を表すもの(存在チェク、ファイル種別、権限等)はキャッシュクリアし都度取得する
* @author Ryuichi TANAKA.
* @since 2016/02/05
* @version 0.7
*/
class File
{
/**
* @var string ファイルパス
*/
private $filePath;
/**
* @var string ファイル名
*/
private $fileName;
/**
* @var string ファイル拡張子
*/
private $fileExt;
/**
* constructor
* @param string $filePath
*/
public function __construct(string $filePath)
{
// realpathを含めてキャッシュクリア
clearstatcache(true);
$this->filePath = $filePath;
$this->fileName = basename($this->filePath);
$this->fileExt = pathinfo($this->filePath, PATHINFO_EXTENSION);
}
/**
* ファイル名を返却する
* @return string ファイル名
*/
public function getFileName()
{
return $this->fileName;
}
/**
* ファイル拡張子を返却する
* @return string ファイル拡張子
*/
public function getFileExtension()
{
return $this->fileExt;
}
/**
* ファイルパスを返却する
* シンボリックリンクの場合、シンボリックリンクファイルパスを返却する
* @return string ファイルパス
*/
public function getFilePath()
{
return $this->filePath;
}
/**
* ファイルパスを返却する
* シンボリックリンクの場合、実ファイルパスを返却する
* @throws IOException
* @return string ファイルパス
*/
public function getAbsoluteFilePath()
{
if ($this->isLink()) {
$filePath = $this->filePath;
while (@is_link($filePath)) {
$linkPath = readlink($filePath);
if ($linkPath === false) {
throw new IOException("Symbolic link read error: " . $filePath);
}
$filePath = $linkPath;
}
$absoluteFilePath = realpath($filePath);
if ($absoluteFilePath === false) {
throw new IOException("File not found: " . $filePath);
}
return $absoluteFilePath;
}
$filePath = $this->getFilePath();
$absoluteFilePath = realpath($filePath);
if ($absoluteFilePath === false) {
throw new IOException("File not found: " . $filePath);
}
return $absoluteFilePath;
}
/**
* 読み込み権限があるか返却する
* @return bool ファイルが存在し、読み込み権限があればtrue
*/
public function isReadable()
{
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
clearstatcache();
return @is_readable($this->filePath);
}
/**
* 書き込み権限があるか返却する
* @return bool ファイルが存在し、書き込み権限があればtrue
*/
public function isWritable()
{
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
clearstatcache();
return @is_writable($this->filePath);
}
/**
* 実行権限があるか返却する
* @return bool ファイルが存在し、実行権限があればtrue
*/
public function isExecutable()
{
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
clearstatcache();
return @is_executable($this->filePath);
}
/**
* ファイルかどうか
* @return bool ファイルならtrue
*/
public function isFile()
{
clearstatcache();
return is_file($this->filePath);
}
/**
* ディレクトリかどうか
* @return bool ディレクトリならtrue
*/
public function isDirectory()
{
clearstatcache();
return is_dir($this->filePath);
}
/**
* リンクかどうか
* @return bool リンクならtrue
*/
public function isLink()
{
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
clearstatcache();
return @is_link($this->filePath);
}
/**
* ファイルサイズを返却する
* ファイルが存在しなければ0
* @return int ファイルサイズ
* @throws IOException
*/
public function length()
{
$length = 0;
if ($this->exists()) {
$filePath = $this->getAbsoluteFilePath();
$length = filesize($filePath);
if ($length === false) {
throw new IOException("Cannot get filesize of " . $filePath);
}
}
return $length;
}
/**
* ファイル(ディレクトリ、リンク含む)が存在するか
* @return bool 存在すればtrue
*/
public function exists()
{
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
clearstatcache();
return $this->isLink() || $this->isDirectory() || $this->isFile();
}
/**
* ファイルを削除する
* @return bool 削除結果
*/
public function delete()
{
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする
clearstatcache();
$isDeleted = false;
if ($this->isWritable()) {
if ($this->isDirectory()) {
$isDeleted = rmdir($this->filePath);
} else {
$isDeleted = unlink($this->filePath);
}
}
return $isDeleted;
}
/**
* ファイルをリネームする
* @param string $destPath 変更後ファイル名
* @return bool リネーム結果
* @throws IOException
*/
public function renameTo(string $destPath)
{
$dirname = dirname($destPath);
$dir = new File($dirname);
if (!$dir->isWritable()) {
throw new IOException("Cannot writable: " . $destPath);
}
$dirPath = $dir->getFilePath();
$absDestPath = $dirPath . "/" . basename($destPath);
return rename($this->filePath, $absDestPath);
}
/**
* ファイル更新日時を返却する
* @return int ファイル更新日時
*/
public function lastModified()
{
return $this->exists() ? filemtime($this->filePath) : 0;
}
}