-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass_file.php
207 lines (194 loc) · 4.27 KB
/
class_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
<?php
/*
This file is part of Mkframework.
Mkframework is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License.
Mkframework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* _file classe pour gerer un fichier
* @author Mika
* @link http://mkf.mkdevs.com/
*/
class _file{
private $_sAdresse;
private $_sContent;
/**
* constructeur
* @access public
* @param string $sAdresse l'adresse du fichier
*/
public function __construct($sAdresse=null){
if($sAdresse!=null){
$this->setAdresse($sAdresse);
}
$this->_sContent=null;
}
/**
* indique que ce n'est pas un repertoire (utilise apres un _file->getList
* @access public
* @return false
*/
public function isDir(){
return false;
}
/**
* indique que c'est un fichier (utilise apres un _file->getList
* @access public
* @return true
*/
public function isFile(){
return true;
}
/**
* defini l'adresse du fichier
* @access public
* @param string $sAdresse adresse du fichier
*/
public function setAdresse($sAdresse){
if($sAdresse!=null){
$this->_sAdresse=$sAdresse;
}
}
/**
* retourne l'adresse complete du fichier
* @access public
* @return string
*/
public function getAdresse(){
return $this->_sAdresse;
}
/**
* initialise le contenu du fichier
* @access public
* @param string $sContent contenu du fichier
*/
public function setContent($sContent){
$this->_sContent=$sContent;
}
/**
* ajoute du contenu au fichier
* @access public
* @param string $sContent contenu du fichier a ajouter
*/
public function addContent($sContent){
$this->_sContent.=$sContent;
}
/**
* sauvegarde le fichier
* @access public
*/
public function save($sOption='w'){
$this->write($this->_sContent,$sOption);
}
/**
* retourne le contenu du fichier
* @access public
* @return string
*/
public function getContent(){
$this->verif();
$sFichier=file_get_contents($this->_sAdresse);
return $sFichier;
}
/**
* charge le contenu du fichier
* @access public
*/
public function load(){
$this->_sContent=$this->getContent();
}
/**
* reinitialise le fichier
* @access public
*/
public function clean(){
$this->_sContent=null;
$this->_sAdresse=null;
}
/**
* retourne le contenu du fichier sous forme d'un tableau
* @access public
* @return array
*/
public function getTab(){
$this->verif();
return file($this->_sAdresse);
}
/**
* supprime le fichier
* @access public
*/
public function delete(){
$this->verif();
unlink($this->_sAdresse);
}
/**
* test l'existence du fichier
* @access public
* @return bool true ou false
*/
public function exist(){
return file_exists($this->_sAdresse);
}
/**
* retourne le nom du fichier
* @access public
* @return string
*/
public function getName(){
$this->verif();
return basename($this->_sAdresse);
}
/**
* retourne l'extension du fichier
* @access public
* @return string
*/
public function getExtension(){
$this->verif();
$tTmp=preg_split('/\./',$this->_sAdresse);
return end($tTmp);
}
/**
* ecrit $sContent avec l'option $sOption
* @access public
* @param string $sContent
* @param string $sOption
*/
public function write($sContent,$sOption='w'){
if($sContent==''){
file_put_contents($this->_sAdresse,$sContent);
}else if(!file_put_contents($this->_sAdresse,$sContent)){
throw new Exception('Can t write "'.$sContent.'"'.$this->_sAdresse);
}
}
/**
* retourne le timestamp de modification du fichier
* @access public
* @return int
*/
public function filemtime(){
return filemtime($this->_sAdresse);
}
/**
* change les droits d'un fichier
* @access public
* @param string $iVal du chmod a faire
*/
public function chmod($iVal=0777){
chmod($this->_sAdresse,$iVal);
}
protected function verif(){
if(!$this->exist()){
throw new Exception($this->_sAdresse.' n\'existe pas');
}
return true;
}
}