1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18: 19: 20:
21:
22:
23:
24: class File_FileManager
25: {
26:
27: const READ = 'r';
28: const READ_BYTE = 'rb';
29: const READ_WRITE = 'r+';
30: const WRITE = 'w';
31: const WRITE_BYTE = 'wb';
32: const READ_WRITE_RESET = 'w+';
33: const ADD = 'a';
34: const READ_WRITE_ADD = 'a+';
35:
36: 37: 38: 39: 40:
41: private $fileHandler = null;
42:
43: 44: 45: 46:
47: private $fileInfo;
48:
49: 50: 51: 52: 53:
54: protected $pathToFile;
55:
56: 57: 58: 59: 60:
61: public $overwrite;
62:
63: 64: 65: 66: 67:
68: public function __construct($file)
69: {
70: $this->pathToFile = $file;
71: $this->fileInfo = new SplFileInfo($this->pathToFile);
72: $this->overwrite = true;
73: }
74:
75: 76: 77: 78: 79:
80: public function isFile()
81: {
82: if($this->fileInfo->isFile())
83: return true;
84: else
85: return false;
86: }
87:
88: 89: 90: 91: 92:
93: public function getInfo()
94: {
95: $arrInfo = array();
96: if($this->fileInfo->isFile())
97: {
98: $arrInfo['extension'] = File_Util::getExtension($this->pathToFile);
99: $arrInfo['name'] = File_Util::getNameFromPath($this->pathToFile);
100: $arrInfo['size'] = $this->fileInfo->getSize();
101: $arrInfo['type'] = 'file';
102:
103: }
104:
105: return $arrInfo;
106: }
107:
108: 109: 110: 111:
112: public function getFileInfo()
113: {
114: return $this->fileInfo;
115: }
116:
117: 118: 119: 120: 121: 122:
123: public function open($mode)
124: {
125: if(file_exists($this->pathToFile) || ($mode != self::READ && $mode != self::READ_WRITE))
126: {
127: if($this->isFile() || ($mode != self::READ && $mode != self::READ_WRITE))
128: {
129: try{
130: $this->fileHandler = $this->fileInfo->openFile($mode);
131: if($this->fileHandler != null)
132: return true;
133: } catch(RuntimeException $e) {
134: return false;
135: }
136:
137: return false;
138: }
139: else
140: {
141: throw new InvalidArgumentException($this->pathToFile.' n\'est pas un fichier valide');
142: }
143: }
144: else
145: {
146: throw new InvalidArgumentException('Le fichier '.$this->pathToFile.' n\'existe pas');
147: }
148: }
149:
150: 151: 152: 153: 154:
155: public function close()
156: {
157: if($this->fileHandler != null)
158: {
159: $this->fileHandler = null;
160: return true;
161: }
162: return false;
163: }
164:
165: 166: 167: 168: 169:
170: public function read()
171: {
172: $datas = "";
173: if($this->fileHandler == null)
174: $this->open (self::READ);
175: if($this->fileInfo->isReadable()) {
176: $this->fileHandler->rewind();
177: while(!$this->fileHandler->eof())
178: $datas .= $this->fileHandler->fgets();
179:
180: return $datas;
181: }
182:
183: return false;
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function readLine($line2read=null)
193: {
194: if($this->fileHandler == null)
195: $this->open (self::READ);
196:
197: $this->fileHandler->rewind();
198:
199: if($line2read == null)
200: {
201: $arrLine = array();
202: foreach($this->fileHandler as $lineNumber => $content)
203: $arrLine[$lineNumber] = $content;
204:
205: return $arrLine;
206: }
207: else
208: {
209: $line2read -= 1;
210: $this->fileHandler->seek($line2read);
211: return $this->fileHandler->current();
212: }
213: return false;
214: }
215:
216: 217: 218: 219: 220: 221:
222: public function search($toSearch)
223: {
224: $search = $this->read();
225: return stripos($search,$toSearch);
226: }
227:
228: 229: 230: 231: 232: 233:
234: public function write($contenu)
235: {
236: if($this->fileHandler !=null && $this->fileInfo->isWritable())
237: {
238: return $this->fileHandler->fwrite($contenu);
239: }
240: return false;
241: }
242:
243: 244: 245: 246: 247: 248: 249:
250: public function writeLine($line,$endLine="\n")
251: {
252: if($this->fileHandler !=null && $this->fileInfo->isWritable())
253: {
254: return $this->fileHandler->fwrite($line.$endLine);
255: }
256: return false;
257: }
258:
259: 260: 261: 262: 263: 264:
265: public function insertLine($line,$numLineInsert)
266: {
267: $lignes = $this->readLine();
268:
269: if($lignes && $this->fileHandler !=null && $this->fileInfo->isWritable())
270: {
271: array_splice($lignes, $numLineInsert, 0,$line."\n");
272: file_put_contents($this->pathToFile, join("",$lignes));
273: }
274: }
275:
276: 277: 278: 279: 280: 281: 282:
283: public function copy($pathToCopy)
284: {
285: $pathToCopy = $this->preparePath($pathToCopy,'copy');
286: if(!$this->overwrite)
287: {
288: if(!file_exists($pathToCopy))
289: {
290: return false;
291: }
292: else
293: {
294: return copy($this->pathToFile,$pathToCopy);
295: }
296: }
297: else
298: {
299: return copy($this->pathToFile,$pathToCopy);
300: }
301: }
302:
303: 304: 305: 306: 307: 308:
309: public function move($pathToMove)
310: {
311: $pathToMove = $this->preparePath($pathToMove,'copy');
312: if(!$this->overwrite)
313: {
314: if(file_exists($pathToMove))
315: {
316: return false;
317: }
318: else
319: {
320: return rename($this->pathToFile,$pathToMove);
321: }
322: }
323: else
324: {
325: if(file_exists($pathToMove))
326: unlink($pathToMove);
327: return rename($this->pathToFile,$pathToMove);
328: }
329: }
330:
331: 332: 333:
334: public function delete()
335: {
336: $this->close();
337: @unlink($this->pathToFile);
338: }
339:
340: 341: 342: 343: 344: 345: 346:
347: private function preparePath($path,$mode)
348: {
349: $path = trim($path);
350: switch($mode)
351: {
352: case 'copy' :
353: if($path[0]=='/')
354: {
355: echo $path = substr($path,1);
356: }
357: break;
358: }
359: return $path;
360: }
361:
362: 363: 364: 365: 366:
367: public function append($data)
368: {
369: if($this->fileHandler == null)
370: return file_put_contents($this->pathToFile, $data, FILE_APPEND);
371: else
372: throw new Exception ('Fichier déjà ouvert, utilisez write()');
373: }
374:
375: 376: 377: 378: 379:
380: public function changePermission($octal)
381: {
382: chmod($this->pathToFile,$octal);
383: }
384:
385: 386: 387: 388: 389: 390:
391: public function getLMTime($dateType = null)
392: {
393: $time = $this->fileInfo->getMTime();
394: if($time)
395: {
396: if($dateType)
397: return date($dateType,$time);
398: return $time;
399: }
400: return false;
401: }
402:
403: public function __destruct()
404: {
405: $this->close();
406: }
407: }
408: ?>