1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\File; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Win\File\Upload\UploadbleInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Arquivos |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class File extends DirectoryItem implements UploadbleInterface { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Instância um novo arquivo |
16
|
|
|
* @param string $path Caminho relativo |
17
|
|
|
*/ |
18
|
|
|
public function __construct($path) { |
19
|
|
|
$this->setPath($path); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** @return string */ |
23
|
|
|
public function getExtension() { |
24
|
|
|
return pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** @return string */ |
28
|
|
|
protected function getExtensionDot() { |
29
|
|
|
return $this->getExtension() ? '.' . $this->getExtension() : ''; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @return string */ |
33
|
|
|
public function getType() { |
34
|
|
|
return mime_content_type($this->getAbsolutePath()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @return int|false */ |
38
|
|
|
public function getSize() { |
39
|
|
|
$size = false; |
40
|
|
|
if ($this->exists()) { |
41
|
|
|
$size = filesize($this->getAbsolutePath()); |
42
|
|
|
} |
43
|
|
|
return $size; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @return boolean */ |
47
|
|
|
public function exists() { |
48
|
|
|
return is_file($this->getAbsolutePath()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $path Caminho relativo |
53
|
|
|
* @throws Exception |
54
|
|
|
*/ |
55
|
|
|
protected function setPath($path) { |
56
|
|
|
if (!preg_match(static::REGEXP_PATH, $path)) { |
57
|
|
|
throw new Exception($path . ' is a invalid file path.'); |
58
|
|
|
} |
59
|
|
|
parent::setPath($path); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @param string $name */ |
63
|
|
|
protected function setName($name) { |
64
|
|
|
if ($name) { |
65
|
|
|
if (!preg_match(static::REGEXP_NAME, $name)) { |
66
|
|
|
throw new Exception($name . ' is a invalid file name.'); |
67
|
|
|
} |
68
|
|
|
$path = $this->getDirectory()->getPath() . DIRECTORY_SEPARATOR . $name . $this->getExtensionDot(); |
69
|
|
|
$this->setPath($path); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @param string $extension */ |
74
|
|
|
protected function setExtension($extension) { |
75
|
|
|
if ($extension) { |
76
|
|
|
$path = $this->getDirectory()->getPath() . DIRECTORY_SEPARATOR . $this->getName() . '.' . $extension; |
77
|
|
|
$this->setPath($path); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Exclui o arquivo |
83
|
|
|
* @return boolean |
84
|
|
|
*/ |
85
|
|
|
public function delete() { |
86
|
|
|
return unlink($this->getAbsolutePath()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Salva o conteúdo no arquivo |
91
|
|
|
* @param string $content |
92
|
|
|
* @param string $mode |
93
|
|
|
* @return boolean |
94
|
|
|
*/ |
95
|
|
|
public function write($content, $mode = 'w') { |
96
|
|
|
$return = false; |
97
|
|
|
if (strlen($this->getName()) > 0) { |
98
|
|
|
$this->getDirectory()->create(); |
99
|
|
|
$fp = fopen($this->getAbsolutePath(), $mode); |
100
|
|
|
if ($fp !== false) { |
101
|
|
|
fwrite($fp, $content); |
102
|
|
|
$return = fclose($fp); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
return $return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Retorna o conteúdo do arquivo |
110
|
|
|
* @param string|false $content |
111
|
|
|
*/ |
112
|
|
|
public function read() { |
113
|
|
|
$content = false; |
114
|
|
|
if ($this->exists()) { |
115
|
|
|
$content = file_get_contents($this->getAbsolutePath()); |
116
|
|
|
} |
117
|
|
|
return $content; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Renomeia o arquivo |
122
|
|
|
* @param string $newName [optional] |
123
|
|
|
* @param string $newExtension [optional] |
124
|
|
|
* @return boolean |
125
|
|
|
*/ |
126
|
|
|
public function rename($newName = null, $newExtension = null) { |
127
|
|
|
$oldPath = $this->getAbsolutePath(); |
128
|
|
|
$this->setExtension($newExtension); |
129
|
|
|
$this->setName($newName); |
130
|
|
|
return rename($oldPath, $this->getAbsolutePath()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|