Classe directoryiterator pour php4

Contenu du snippet

Dans le cadre d'un de mes projets, j'utilise la classe PHP 5 DirectoryIterator vraiment utile pour le parcours de dossier.
Elle utilise les itérateurs et permet donc un parcours plus simple (ca évite de faire des vérifications dans les boucles, contrairement à la classe dir).
Le soucis, c'est qu'elle n'est QUE PHP 5. Je l'ai donc implémentée en PHP 4 afin d'avoir un code fonctionnel, même sur un serveur en PHP 4.

Je vous laisse voir le code :)

Source / Exemple :


<?php
/**

  • Projet : Directory
  • File : DirectoryIterator.class.php
*
  • class DirectoryIterator
  • @author Codefalse <codefalse [at] altern [dot] org>
  • @copyright Codefalse
  • @version 20070529
  • @licence GPL - http://www.gnu.org/licenses/gpl.html - General Public Licence
  • @version 1.0.0
  • /
class DirectoryIterator { /**
  • Orignal Path
*
  • @var string
  • /
var $_sRoot; /**
  • Opendir ressource
*
  • @var ressource
  • /
var $_rOpenDir; /**
  • Current File
*
  • @var string
  • /
var $_sCurrentFile; /**
  • Starting Offset
*
  • @var int
  • /
var $_iOffset = -1; /**
  • function DirectoryIterator
  • Constructor
  • Open the path given and start the reading
*
  • @param string $sPath : The path. Need to finish by / (DIRECTORY_SEPARATOR)
*
  • @return void
  • /
function DirectoryIterator ($sPath) { $this->_sRoot = $sPath; clearstatcache (); if (is_dir ($sPath) === true) { $this->_rOpenDir = opendir ($sPath); $this->next(); } else { die ('Invalid Path !'); } } /**
  • function current
  • Get the current element value
*
  • @return string
  • /
// @return DirectoryIterator ?! function current () { return $this->_sCurrentFile; } /**
  • function next
  • Move to next entry
*
  • @return void
  • /
function next () { $this->_sCurrentFile = readdir ($this->_rOpenDir); $this->_iOffset++; } /**
  • function rewind
  • Rewind dir back to the start
*
  • @return void
  • /
function rewind () { $this->_sCurrentFile = rewinddir ($this->_rOpenDir); $this->_iOffset = -1; $this->next(); } /**
  • function valid
  • Check whether dir contains more entries
*
  • @return string
  • /
function valid () { if ($this->_sCurrentFile === false) return false; else return true; } /**
  • function key
  • Return current dir entry
*
  • @return string
  • /
// @return string ?! function key () { return $this->_iOffset; } /**
  • function isDir
  • Returns true if file is directory
*
  • @return boolean
  • /
function isDir () { clearstatcache (); return is_dir ($this->_sRoot.DIRECTORY_SEPARATOR.$this->_sCurrentFile); } /**
  • function isDot
  • Returns true if current entry is '.' or '..'
*
  • @return boolean
  • /
function isDot () { if ($this->_sCurrentFile == '.' || $this->_sCurrentFile == '..') return true; else return false; } /**
  • function getATime
  • return last access time of file
*
  • @return int
  • /
function getATime () { clearstatcache (); return fileatime ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getCTime
  • Return inode modification time of file
*
  • @return int
  • /
function getCTime () { clearstatcache (); return filectime ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getGroup
  • Return file group
*
  • @return int
  • /
function getGroup () { clearstatcache (); return filegroup ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getInode
  • Return file inode
*
  • @return int
  • /
function getInode () { clearstatcache (); return fileinode ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getMTime
  • Return last modification time of file
*
  • @return int
  • /
function getMTime () { clearstatcache (); return filemtime ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getOwner
  • Return file owner
*
  • @return int
  • /
function getOwner () { clearstatcache (); return fileowner ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getPerms
  • Return file permissions
*
  • @return int
  • /
function getPerms () { clearstatcache (); return fileperms ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getSize
  • Return file size
*
  • @return int
  • /
function getSize () { clearstatcache (); if (is_file ($this->_sRoot.$this->_sCurrentFile) === true) return filesize ($this->_sRoot.$this->_sCurrentFile); else return -1; } /**
  • function getFilename
  • Return filename of current dir entry
*
  • @return string
  • /
function getFilename () { return $this->_sCurrentFile; } /**
  • function getPath
  • Return directory path
*
  • @return string
  • /
function getPath () { return $this->_sRoot; } /**
  • function getPathname
  • Return path and filename of current dir entry
*
  • @return string
  • /
function getPathname () { return $this->_sRoot.$this->_sCurrentFile; } /**
  • function getType
  • Return file type
*
  • @return string
  • /
function getType () { clearstatcache (); return filetype ($this->_sRoot.$this->_sCurrentFile); } /**
  • function isExecutable
  • Returns true if file is executable
*
  • @return boolean
  • /
function isExecutable () { clearstatcache (); return is_executable ($this->_sRoot.$this->_sCurrentFile); } /**
  • function isFile
  • Returns true if file is a regular file
*
  • @return boolean
  • /
function isFile () { clearstatcache (); return is_file ($this->_sRoot.$this->_sCurrentFile); } /**
  • function isLink
  • Returns true if file is symbolic link
*
  • @return boolean
  • /
function isLink () { clearstatcache (); return is_link ($this->_sRoot.$this->_sCurrentFile); } /**
  • function isReadable
  • Returns true if file can be read
*
  • @return boolean
  • /
function isReadable () { clearstatcache (); return is_readable ($this->_sRoot.$this->_sCurrentFile); } /**
  • function isWritable
  • Returns true if file can be written
*
  • @return boolean
  • /
function isWritable () { clearstatcache (); return is_writable ($this->_sRoot.$this->_sCurrentFile); } /**
  • function getChildren
  • Returns the current entry if it is a directory
*
  • @return DirectoryIterator
  • /
function getChildren () { if (is_dir ($this->_sRoot.$this->_sCurrentFile) === true) return new DirectoryIterator ($this->_sRoot.$this->_sCurrentFile.DIRECTORY_SEPARATOR); } } ?>

Conclusion :


Je n'ai pas testé toutes les fonctions donc il se peut qu'il subsiste des bugs. Les fonctions sont très simples donc normallement non, mais on sais jamais :)

Les commentaires sont bien évidement les bienvenus :)

A voir également