Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • File_Crypt
  • File_FileCSV
  • File_FileManager
  • File_FolderManager
  • File_Upload
  • File_Util
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pry Framework
  4:  *
  5:  * LICENSE
  6:  *
  7:  * This source file is subject to the new BSD license that is bundled
  8:  * with this package in the file LICENSE.txt.
  9:  * 
 10:  * @version $Revision: 276 $
 11:  */
 12: 
 13: /**
 14:  * Gestion d'accès au fichier
 15:  * @category Pry
 16:  * @package File
 17:  * @version 1.2.0
 18:  * @author Olivier ROGER <oroger.fr>      
 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:      * Ressource de fichier
 38:      * @access private
 39:      * @var SplFileObject
 40:      */
 41:     private $fileHandler = null;
 42:     
 43:     /**
 44:      * Information de fichier
 45:      * @var SplFileInfo
 46:      */
 47:     private $fileInfo;
 48:     
 49:     /**
 50:      * Chemin vers le fichier d'origine
 51:      * @access protected
 52:      * @var string
 53:      */
 54:     protected $pathToFile;
 55:     
 56:     /**
 57:      * Détermine is un fichier doit être réecris ou non
 58:      * @access public
 59:      * @var boolean
 60:      */
 61:     public $overwrite;
 62:     
 63:     /**
 64:      * Constructeur
 65:      *
 66:      * @param string $file chemin vers le fichier
 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:      * Vérifie si le fichier est un fichier
 77:      *
 78:      * @return boolean
 79:      */
 80:     public function isFile()
 81:     {
 82:         if($this->fileInfo->isFile())
 83:             return true;
 84:         else
 85:             return false;
 86:     }
 87:     
 88:     /**
 89:      * Récupère les information du fichier
 90:      *
 91:      * @return array
 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:      * 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:             $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:      * Lit un fichier ligne par ligne
188:      *
189:      * @param int $line2read Ligne à lire dans le fichier
190:      * @return mixed
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; //ITérateur commence à 0;
210:             $this->fileHandler->seek($line2read);
211:             return $this->fileHandler->current();
212:         }
213:         return false;
214:     }
215:     
216:     /**
217:      * Recherche si une chaine est présente dans un fichier
218:      *
219:      * @param string $toSearch
220:      * @return mixed
221:      */
222:     public function search($toSearch)
223:     {
224:         $search = $this->read();
225:         return stripos($search,$toSearch);
226:     }
227:     
228:     /**
229:      * Ecrit dans le fichier
230:      *
231:      * @param string $contenu
232:      * @return mixed False/null si erreur , nombre d'octet sinon
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:      * Ecrit une ligne
245:      *
246:      * @param string $ligne Ligne à écrire
247:      * @param string $endLine caractère de fin de ligne
248:      * @return mixed False si erreur , nombre d'octec sinon
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:      * Insert une ligne dans un fichier à l'endroit voulu
261:      *
262:      * @param string $line Ligne à écrire
263:      * @param int $numLineInsert Indice de ligne pour insérer la nouvelle ligne
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:      * Copie un fichier vers un dossier
278:      *
279:      * @param string $pathToCopy Chemin complet du fichier de copié
280:      * @param boolean $ecrase
281:      * @return unknown
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:      * Déplace un fichier vers un dossier
305:      *
306:      * @param string $pathToMove Chemin complet du fichier une fois déplacé
307:      * @return boolean
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:      * Supprime le fichier
333:      */
334:     public function delete()
335:     {
336:         $this->close();
337:         @unlink($this->pathToFile);
338:     }
339:     
340:     /**
341:      * Prépare le path donné pour être correct (slash, espace ...)
342:      *
343:      * @param string $path
344:      * @param string $mode Mode de préparation (copy,move ...)
345:      * @return string
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:      * Ajoute des données à un fichier existant
364:      * @since 1.0.6
365:      * @param string $data
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:      * Change les permissions d'un fichier
377:      * @since 1.0.6
378:      * @param octal $octal Droits en octal : 0644; 0777 ...
379:      */
380:     public function changePermission($octal)
381:     {
382:         chmod($this->pathToFile,$octal);
383:     }
384:     
385:     /**
386:      * Retourne la date de dernière modification
387:      * @since 1.0.6
388:      * @param string $dateType Format de la date retour. Timestamp si null
389:      * @return mixed
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:  ?>
Pry Framework API documentation generated by ApiGen 2.6.1