Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Crypt
  • FileCSV
  • FileManager
  • FolderManager
  • Upload
  • Util
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\File;
 14: 
 15: use Pry\File\Util;
 16: 
 17: /**
 18:  * Gestion d'accès au fichier
 19:  * @category Pry
 20:  * @package File
 21:  * @version 1.2.0
 22:  * @author Olivier ROGER <oroger.fr>      
 23:  *
 24:  */
 25: class FileManager
 26: {
 27: 
 28:     const READ             = 'r';
 29:     const READ_BYTE        = 'rb';
 30:     const READ_WRITE       = 'r+';
 31:     const WRITE            = 'w';
 32:     const WRITE_BYTE       = 'wb';
 33:     const READ_WRITE_RESET = 'w+';
 34:     const ADD              = 'a';
 35:     const READ_WRITE_ADD   = 'a+';
 36: 
 37:     /**
 38:      * Ressource de fichier
 39:      * @access private
 40:      * @var SplFileObject
 41:      */
 42:     private $fileHandler = null;
 43: 
 44:     /**
 45:      * Information de fichier
 46:      * @var SplFileInfo
 47:      */
 48:     private $fileInfo;
 49: 
 50:     /**
 51:      * Chemin vers le fichier d'origine
 52:      * @access protected
 53:      * @var string
 54:      */
 55:     protected $pathToFile;
 56: 
 57:     /**
 58:      * Détermine is un fichier doit être réecris ou non
 59:      * @access public
 60:      * @var boolean
 61:      */
 62:     public $overwrite;
 63: 
 64:     /**
 65:      * Constructeur
 66:      *
 67:      * @param string $file chemin vers le fichier
 68:      */
 69:     public function __construct($file)
 70:     {
 71:         $this->pathToFile = $file;
 72:         $this->fileInfo   = new \SplFileInfo($this->pathToFile);
 73:         $this->overwrite  = true;
 74:     }
 75: 
 76:     /**
 77:      * Vérifie si le fichier est un fichier
 78:      *
 79:      * @return boolean
 80:      */
 81:     public function isFile()
 82:     {
 83:         if ($this->fileInfo->isFile())
 84:             return true;
 85:         else
 86:             return false;
 87:     }
 88: 
 89:     /**
 90:      * Récupère les information du fichier
 91:      *
 92:      * @return array
 93:      */
 94:     public function getInfo()
 95:     {
 96:         $arrInfo = array();
 97:         if ($this->fileInfo->isFile())
 98:         {
 99:             $arrInfo['extension'] = Util::getExtension($this->pathToFile);
100:             $arrInfo['name']      = Util::getNameFromPath($this->pathToFile);
101:             $arrInfo['size']      = $this->fileInfo->getSize();
102:             $arrInfo['type']      = 'file';
103:         }
104: 
105:         return $arrInfo;
106:     }
107: 
108:     /**
109:      * Retourne l'objet file info
110:      * @return SplFileInfo
111:      */
112:     public function getFileInfo()
113:     {
114:         return $this->fileInfo;
115:     }
116: 
117:     /**
118:      * Ouvre le fichier demandé
119:      *
120:      * @param string $mode Mode d'ouverture
121:      * @return boolean
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:      * Ferme le fichier
152:      *
153:      * @return boolean
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:      * Lit le contenu d'un fichier
167:      *
168:      * @return mixed
169:      */
170:     public function read()
171:     {
172:         $datas = "";
173:         if ($this->fileHandler == null)
174:             $this->open(self::READ);
175:         if ($this->fileInfo->isReadable())
176:         {
177:             $this->fileHandler->rewind();
178:             while (!$this->fileHandler->eof())
179:                 $datas .= $this->fileHandler->fgets();
180: 
181:             return $datas;
182:         }
183: 
184:         return false;
185:     }
186: 
187:     /**
188:      * Lit un fichier ligne par ligne
189:      *
190:      * @param int $line2read Ligne à lire dans le fichier
191:      * @return mixed
192:      */
193:     public function readLine($line2read = null)
194:     {
195:         if ($this->fileHandler == null)
196:             $this->open(self::READ);
197: 
198:         $this->fileHandler->rewind();
199: 
200:         if ($line2read == null)
201:         {
202:             $arrLine = array();
203:             foreach ($this->fileHandler as $lineNumber => $content)
204:                 $arrLine[$lineNumber] = $content;
205: 
206:             return $arrLine;
207:         }
208:         else
209:         {
210:             $line2read -= 1; //ITérateur commence à 0;
211:             $this->fileHandler->seek($line2read);
212:             return $this->fileHandler->current();
213:         }
214:         return false;
215:     }
216: 
217:     /**
218:      * Recherche si une chaine est présente dans un fichier
219:      *
220:      * @param string $toSearch
221:      * @return mixed
222:      */
223:     public function search($toSearch)
224:     {
225:         $search = $this->read();
226:         return stripos($search, $toSearch);
227:     }
228: 
229:     /**
230:      * Ecrit dans le fichier
231:      *
232:      * @param string $contenu
233:      * @return mixed False/null si erreur , nombre d'octet sinon
234:      */
235:     public function write($contenu)
236:     {
237:         if ($this->fileHandler != null && $this->fileInfo->isWritable())
238:         {
239:             return $this->fileHandler->fwrite($contenu);
240:         }
241:         return false;
242:     }
243: 
244:     /**
245:      * Ecrit une ligne
246:      *
247:      * @param string $ligne Ligne à écrire
248:      * @param string $endLine caractère de fin de ligne
249:      * @return mixed False si erreur , nombre d'octec sinon
250:      */
251:     public function writeLine($line, $endLine = "\n")
252:     {
253:         if ($this->fileHandler != null && $this->fileInfo->isWritable())
254:         {
255:             return $this->fileHandler->fwrite($line . $endLine);
256:         }
257:         return false;
258:     }
259: 
260:     /**
261:      * Insert une ligne dans un fichier à l'endroit voulu
262:      *
263:      * @param string $line Ligne à écrire
264:      * @param int $numLineInsert Indice de ligne pour insérer la nouvelle ligne
265:      */
266:     public function insertLine($line, $numLineInsert)
267:     {
268:         $lignes = $this->readLine();
269: 
270:         if ($lignes && $this->fileHandler != null && $this->fileInfo->isWritable())
271:         {
272:             array_splice($lignes, $numLineInsert, 0, $line . "\n");
273:             file_put_contents($this->pathToFile, join("", $lignes));
274:         }
275:     }
276: 
277:     /**
278:      * Copie un fichier vers un dossier
279:      *
280:      * @param string $pathToCopy Chemin complet du fichier de copié
281:      * @param boolean $ecrase
282:      * @return unknown
283:      */
284:     public function copy($pathToCopy)
285:     {
286:         $pathToCopy = $this->preparePath($pathToCopy, 'copy');
287:         if (!$this->overwrite)
288:         {
289:             if (!file_exists($pathToCopy))
290:             {
291:                 return false;
292:             }
293:             else
294:             {
295:                 return copy($this->pathToFile, $pathToCopy);
296:             }
297:         }
298:         else
299:         {
300:             return copy($this->pathToFile, $pathToCopy);
301:         }
302:     }
303: 
304:     /**
305:      * Déplace un fichier vers un dossier
306:      *
307:      * @param string $pathToMove Chemin complet du fichier une fois déplacé
308:      * @return boolean
309:      */
310:     public function move($pathToMove)
311:     {
312:         $pathToMove = $this->preparePath($pathToMove, 'copy');
313:         if (!$this->overwrite)
314:         {
315:             if (file_exists($pathToMove))
316:             {
317:                 return false;
318:             }
319:             else
320:             {
321:                 return rename($this->pathToFile, $pathToMove);
322:             }
323:         }
324:         else
325:         {
326:             if (file_exists($pathToMove))
327:                 unlink($pathToMove);
328:             return rename($this->pathToFile, $pathToMove);
329:         }
330:     }
331: 
332:     /**
333:      * Supprime le fichier
334:      */
335:     public function delete()
336:     {
337:         $this->close();
338:         @unlink($this->pathToFile);
339:     }
340: 
341:     /**
342:      * Prépare le path donné pour être correct (slash, espace ...)
343:      *
344:      * @param string $path
345:      * @param string $mode Mode de préparation (copy,move ...)
346:      * @return string
347:      */
348:     private function preparePath($path, $mode)
349:     {
350:         $path = trim($path);
351:         switch ($mode)
352:         {
353:             case 'copy' :
354:                 if ($path[0] == '/')
355:                 {
356:                     echo $path = substr($path, 1);
357:                 }
358:                 break;
359:         }
360:         return $path;
361:     }
362: 
363:     /**
364:      * Ajoute des données à un fichier existant
365:      * @since 1.0.6
366:      * @param string $data
367:      */
368:     public function append($data)
369:     {
370:         if ($this->fileHandler == null)
371:             return file_put_contents($this->pathToFile, $data, FILE_APPEND);
372:         else
373:             throw new \Exception('Fichier déjà ouvert, utilisez write()');
374:     }
375: 
376:     /**
377:      * Change les permissions d'un fichier
378:      * @since 1.0.6
379:      * @param octal $octal Droits en octal : 0644; 0777 ...
380:      */
381:     public function changePermission($octal)
382:     {
383:         chmod($this->pathToFile, $octal);
384:     }
385: 
386:     /**
387:      * Retourne la date de dernière modification
388:      * @since 1.0.6
389:      * @param string $dateType Format de la date retour. Timestamp si null
390:      * @return mixed
391:      */
392:     public function getLMTime($dateType = null)
393:     {
394:         $time = $this->fileInfo->getMTime();
395:         if ($time)
396:         {
397:             if ($dateType)
398:                 return date($dateType, $time);
399:             return $time;
400:         }
401:         return false;
402:     }
403: 
404:     public function __destruct()
405:     {
406:         $this->close();
407:     }
408: 
409: }
410: 
411: ?>
Pry API documentation generated by ApiGen 2.8.0