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 {
/**
*
var $_sRoot;
/**
*
var $_rOpenDir;
/**
*
var $_sCurrentFile;
/**
*
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)
*
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 DirectoryIterator ?!
function current () {
return $this->_sCurrentFile;
}
/**
- function next
- Move to next entry
*
function next () {
$this->_sCurrentFile = readdir ($this->_rOpenDir);
$this->_iOffset++;
}
/**
- function rewind
- Rewind dir back to the start
*
function rewind () {
$this->_sCurrentFile = rewinddir ($this->_rOpenDir);
$this->_iOffset = -1;
$this->next();
}
/**
- function valid
- Check whether dir contains more entries
*
function valid () {
if ($this->_sCurrentFile === false)
return false;
else
return true;
}
/**
- function key
- Return current dir entry
*
// @return string ?!
function key () {
return $this->_iOffset;
}
/**
- function isDir
- Returns true if file is directory
*
function isDir () {
clearstatcache ();
return is_dir ($this->_sRoot.DIRECTORY_SEPARATOR.$this->_sCurrentFile);
}
/**
- function isDot
- Returns true if current entry is '.' or '..'
*
function isDot () {
if ($this->_sCurrentFile == '.' || $this->_sCurrentFile == '..')
return true;
else return false;
}
/**
- function getATime
- return last access time of file
*
function getATime () {
clearstatcache ();
return fileatime ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function getCTime
- Return inode modification time of file
*
function getCTime () {
clearstatcache ();
return filectime ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function getGroup
- Return file group
*
function getGroup () {
clearstatcache ();
return filegroup ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function getInode
- Return file inode
*
function getInode () {
clearstatcache ();
return fileinode ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function getMTime
- Return last modification time of file
*
function getMTime () {
clearstatcache ();
return filemtime ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function getOwner
- Return file owner
*
function getOwner () {
clearstatcache ();
return fileowner ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function getPerms
- Return file permissions
*
function getPerms () {
clearstatcache ();
return fileperms ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function getSize
- Return file size
*
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
*
function getFilename () {
return $this->_sCurrentFile;
}
/**
- function getPath
- Return directory path
*
function getPath () {
return $this->_sRoot;
}
/**
- function getPathname
- Return path and filename of current dir entry
*
function getPathname () {
return $this->_sRoot.$this->_sCurrentFile;
}
/**
- function getType
- Return file type
*
function getType () {
clearstatcache ();
return filetype ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function isExecutable
- Returns true if file is executable
*
function isExecutable () {
clearstatcache ();
return is_executable ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function isFile
- Returns true if file is a regular file
*
function isFile () {
clearstatcache ();
return is_file ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function isLink
- Returns true if file is symbolic link
*
function isLink () {
clearstatcache ();
return is_link ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function isReadable
- Returns true if file can be read
*
function isReadable () {
clearstatcache ();
return is_readable ($this->_sRoot.$this->_sCurrentFile);
}
/**
- function isWritable
- Returns true if file can be written
*
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 :)