Accueil > > > [PHP5] SMARTDIR : LES ITÉRATEURS EN PHP - LECTURE INTELLIGENTE DE RÉPERTOIRE
[PHP5] SMARTDIR : LES ITÉRATEURS EN PHP - LECTURE INTELLIGENTE DE RÉPERTOIRE
Information sur la source
Description
J'ai décidé de montrer toute la puissance de la SPL (Standard PHP Library) et des itérateurs en PHP5. Ce package est à personnaliser. Il permet la lecture récursive ou non de répertoire, en appliquant, ou non, des filtres. FILTER_ON fait un filtre montrant uniquement les fichiers correspondant aux masques : oSmartDir::FILTER_ON = 'php'; Va filtrer sur les fichiers contenant'php'. oSmartDir::FILTER_ON = array ('php, 'html', 'js'); va filtrer sur les fichiers contenant php, html, ou js. FILTER_OFF faut un filtre négatif : oSmartDir::FILTER_OFF = 'php'; ne montrera que les fichiers ne contenant pas 'php'. Peut aussi être un tableau de masques. oSmartDir::DIr = false; Ne lira pas les répertoire oSmartDir::FILE = false; ne montrera pas les fichiers oSmartDir::RECURSE = false; Ne lira pas récursivement. On peut évidemment modifier ces filtres via la méthode myFilter::valid () Méthodes : oSmartDir::getDir (); permet simplement de récupérer l'itérateur pour l'arborescence définie via les filtres (ou non, d'ailleurs). Plus tard, j'implémenterai de nouvelles méthodes "outils" : copie, déplacement etc...en tenant compte des filtres. ////// J'ai mis une méthode copy (). Attention, elle est effective sur le fichier d'exemple : le zip contient toute une arborescence. Le fichier exemple lit cette arbo avec divers filtres, pour donner quelques exemples, puis crée un répertoire copie/ dans lequel il copie toute l'arborescence, mais en ne prenan t en compte que les fichiers contenant 'php'. ////// J'ai volontairement montré un package assez complexe...mais la lecture d'un répertoire de manière récursive peut se faire en 3 lignes grâce aux itérateurs : <?php $dir = new RecursiveIteratorIterator( new RecursiveDirectoryIterator('.'), true); foreach ( $dir as $file ) { echo str_repeat('-', $dir->getDepth()) . ' '.$file.'<br />'; } ?> Essayez ce code : il va lire récursivement le répertoire courant (donc, avec ses sous-répertoires), tout en indentant en fonction de l'arborescence. Si si...!
Source
- <?php
- /**
- * smartDir package
- * @author Johan Barbier <johan.barbier@gmail.com>
- * @version 20011120
- */
-
- /**
- * class myFilter
- * check active filters
- * RecursiveFilterIterator child
- */
- class myFilter extends RecursiveFilterIterator {
-
- /**
- * active filter ON (keep these)
- *
- * @var mixed
- */
- private $mFilterOn;
-
- /**
- * active filter OFF (do not keep these)
- *
- * @var mixed
- */
- private $mFilterOff;
-
- /**
- * iterator
- *
- * @var iterator
- */
- private $it = null;
-
- /**
- * error message
- *
- * @var string
- */
- const ERROR_NO_VALID_FILTER = '{__FILTER__} is not a valid setable filter';
-
- /**
- * Constructor
- *
- * @param iterator $it
- * @param mixed $mFilterOn
- * @param mixed $mFilterOff
- */
- public function __construct ($it, $mFilterOn, $mFilterOff) {
- parent::__construct ($it);
- $this -> it = $it;
- $this -> mFilterOn = $mFilterOn;
- $this -> mFilterOff = $mFilterOff;
- }
-
- /**
- * Function accept ()
- * returns true or false if the current element is accepted or not
- *
- * @return boolean
- */
- public function accept () {
- if (!is_null ($this -> mFilterOn)) {
- if (!is_array ($this -> mFilterOn)) {
- $mPos = strpos ($this -> it -> getFileName (), (string)$this -> mFilterOn);
- if (false === $mPos) {
- return false;
- }
- } else {
- foreach ($this -> mFilterOn as $sFilter) {
- $mPos = strpos ($this -> it -> getFileName (), (string)$sFilter);
- if (false !== $mPos) {
- return true;
- }
- }
- return false;
- }
- }
- if (!is_null ($this -> mFilterOff)) {
- if (!is_array ($this -> mFilterOff)) {
- $mPos = strpos ($this -> it -> getFileName (), (string)$this -> mFilterOff);
- if (false !== $mPos) {
- return false;
- }
- } else {
- foreach ($this -> mFilterOff as $sFilter) {
- $mPos = strpos ($this -> it -> getFileName (), (string)$sFilter);
- if (false !== $mPos) {
- return false;
- } else {
- return true;
- }
- }
- return false;
- }
- }
- return true;
- }
- }
-
- /**
- * Class MyRecursiveDirectoryIterator
- * parent of main class, RecursiveDirectoryIterator child
- *
- */
- class MyRecursiveDirectoryIterator extends RecursiveDirectoryIterator {
-
- /**
- * are dir valid
- *
- * @var boolean
- */
- protected $bDir = true;
-
- /**
- * are files valid
- *
- * @var boolean
- */
- protected $bFile = true;
-
- /**
- * filter ON (keep these)
- *
- * @var mixed
- */
- protected $mFilterOn = null;
-
- /**
- * filter OFF (do not keep these)
- *
- * @var mixed
- */
- protected $mFilterOff = null;
-
- /**
- * Recursive dir or not
- *
- * @var boolean
- */
- protected $bRecurse = true;
-
- /**
- * props that can be set
- *
- * @var array
- */
- protected $aCanBeSet = array (
- 'FLAG',
- 'FILTER_ON',
- 'FILTER_OFF',
- 'DIR',
- 'FILE',
- 'PATH',
- 'RECURSE'
-
- );
- /**
- * filter ON (keep these)
- *
- * @var RecursiveDirectoryIterator class constant
- */
- protected $cFlag = 0;
-
- /**
- * path to be read
- *
- * @var string (valid path)
- */
- protected $sPath;
-
- protected $sCreatedDir = null;
- /**
- * error messages
- *
- * @var string
- */
- const ERROR_PATH_NOT_FOUND = '{__PATH__} has not been found';
- const ERROR_PROP_NOT_SETABLE = '{__PROP__} is not a setable property';
- const ERROR_BAD_PROP_VALUE = '{__VAL__} is not a correct value for {__PROP__}';
- const ERROR_NO_BOOLEAN = '{__PROP__} value must be a boolean';
-
- /**
- * getChildren will retrieve sub path
- *
- * @return subiterator
- */
- public function getChildren () {
- $iSub = new self ($this -> getPathname ());
- $iSub -> bDir = $this -> bDir;
- $iSub -> bFile = $this -> bFile;
- $iSub -> mFilterOn = $this -> mFilterOn;
- $iSub -> mFilterOff = $this -> mFilterOff;
- return $iSub;
- }
-
- /**
- * get current key
- *
- * @return string
- */
- public function key () {
- return $this -> getPath ();
- }
-
- /**
- * get current file
- *
- * @return string
- */
- public function current () {
- return $this -> getFileName ();
- }
-
- /**
- * is the current element valid or not
- *
- * @return boolean
- */
- public function valid () {
- if (!is_null ($this -> sCreatedDir)) {
- if ($this -> current () === $this -> sCreatedDir) {
- return false;
- }
- }
- $oFilter = new myFilter ($this, $this -> mFilterOn, $this -> mFilterOff);
- if (true === parent::valid ()) {
- if (false === $oFilter -> accept ()) {
- if (false === $this -> isDir ()) {
- parent::next ();
- return $this -> valid ();
- } else {
- return true;
- }
- }
- if (false === $this -> bDir) {
- if (true === $this -> isDir ()) {
- parent::next ();
- return $this -> valid ();
- }
- }
- if (false === $this -> bFile) {
- if (true === $this -> isFile ()) {
- parent::next ();
- return $this -> valid ();
- }
- }
- return true;
- }
- return false;
- }
-
- /**
- * Setter
- *
- * @param string $sProp
- * @param mixed $mVal
- */
- public function __set ($sProp, $mVal) {
- if (!in_array ($sProp, $this -> aCanBeSet)) {
- throw new Exception (str_replace ('{__PATH__}', $sProp, self::ERROR_PROP_NOT_SETABLE));
- }
- switch ($sProp) {
- case 'FLAG' :
- if (!in_array ($mVal, array (parent::CURRENT_AS_FILEINFO, parent::KEY_AS_FILENAME, parent::NEW_CURRENT_AND_KEY, 0))) {
- throw new Exception (str_replace (array ('{__VAL__}', '{__PROP__}'), array ($mVal, $sProp), self::ERROR_PATH_NOT_FOUND));
- }
- $this -> cFlag = $mVal;
- break;
- case 'FILTER_ON' :
- $this -> mFilterOn = $mVal;
- $this -> mFilterOff = null;
- break;
- case 'FILTER_OFF' :
- $this -> mFilterOff = $mVal;
- $this -> mFilterOn = null;
- break;
- case 'DIR' :
- if (!is_bool ($mVal)) {
- throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_NO_BOOLEAN));
- }
- $this -> bDir = $mVal;
- break;
- case 'FILE' :
- if (!is_bool ($mVal)) {
- throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_NO_BOOLEAN));
- }
- $this -> bFile = $mVal;
- break;
- case 'RECURSE' :
- if (!is_bool ($mVal)) {
- throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_NO_BOOLEAN));
- }
- $this -> bRecurse = $mVal;
- break;
- case 'PATH' :
- if (!is_dir ($mVal)) {
- throw new Exception (str_replace ('{__PATH__}', $mVal, self::ERROR_PATH_NOT_FOUND));
- }
- $this -> sPath = $mVal;
- break;
- }
- }
- }
-
- /**
- * Class oSmartDir
- * MyRecursiveDirectoryIterator child
- *
- */
- class oSmartDir extends MyRecursiveDirectoryIterator {
-
- /**
- * Constructor
- *
- * @param string $sPath (valid path)
- */
-
- const ERROR_COPY_FAILED = 'Failed to copy {__FROM__} to {__TO__}';
-
- public function __construct ($sPath) {
- if (!is_dir ($sPath)) {
- throw new Exception (str_replace ('{__PATH__}', $sPath, self::ERROR_PATH_NOT_FOUND));
- }
- $this -> sPath = $sPath;
- }
-
- /**
- * getDir will retrieve the asked directory
- *
- * @return iterator
- */
- public function getDir () {
- parent::__construct ($this -> sPath, $this -> cFlag);
- if (true === $this -> bRecurse) {
- return new RecursiveIteratorIterator ($this, true);
- } else {
- return $this;
- }
- }
-
- public function copy ($sTo) {
- $aDir = $this -> getDir ();
- if (!is_dir ($sTo)) {
- mkdir ($sTo, '0755');
- }
- $this -> sCreatedDir = $sTo;
- while ($aDir -> valid ()) {
- if ($aDir -> current () !== $sTo && !$aDir -> isDot ()) {
- if (!$aDir -> isDir ()) {
- if (!@copy ($aDir -> getPathName (), $sTo.'/'.$aDir -> getPathName ())) {
- throw new Exception (str_replace (array ('{__FROM__}', '{__TO__}'), array ($aDir -> getPathName (), $sTo.'/'.$aDir -> getPathName ()), self::ERROR_COPY_FAILED));
- }
- } else {
- if (!@mkdir ($sTo.'/'.$aDir -> getPathName (), '0755')) {
- throw new Exception (str_replace (array ('{__FROM__}', '{__TO__}'), array ($aDir -> getPathName (), $sTo.'/'.$aDir -> getPathName ()), self::ERROR_COPY_FAILED));
- }
- }
- }
- $aDir -> next ();
- }
- $this -> sCreatedDir = null;
- }
- }
-
-
- /**
- * STARTING EXAMPLES
- */
- try {
- $oDir = new oSmartDir ('.');
- } catch (Exception $e) {
- echo $e -> getMessage ();
- }
- try {
-
- echo '<br /><strong>ALL NO RECURSIVE</strong><br />';
- $oDir -> RECURSE = false; // no recursivity
- $aDir = $oDir -> getDir ();
- while ($aDir -> valid ()) {
- if ($aDir -> isDot ()) {
- $aDir -> next ();
- }
- $sHtml = '';
- if ($aDir -> isDir ()) {
- $sHtml.= '<strong>'.$aDir -> current ().'</strong>';
- } else {
- $sHtml.= '<em>'.$aDir -> current ().'</em>';
- }
- $sHtml .= '<br />';
- echo $sHtml;
- $aDir -> next ();
- }
-
- echo '<br /><strong>ALL</strong><br />';
- $oDir -> RECURSE = true; // recursivity back to true
- $aDir = $oDir -> getDir ();
- while ($aDir -> valid ()) {
- if ($aDir -> isDot ()) {
- $aDir -> next ();
- }
- $sHtml = str_repeat (' ', $aDir -> getDepth ());
- if ($aDir -> isDir ()) {
- $sHtml.= '<strong>'.$aDir -> current ().'</strong>';
- } else {
- $sHtml.= '<em>'.$aDir -> current ().'</em>';
- }
- $sHtml .= '<br />';
- echo $sHtml;
- $aDir -> next ();
- }
-
- echo '<br /><strong>ALL + fileSize</strong><br />';
- $oDir -> RECURSE = true; // recursivity back to true
- $aDir = $oDir -> getDir ();
- while ($aDir -> valid ()) {
- if ($aDir -> isDot ()) {
- $aDir -> next ();
- }
- $sHtml = str_repeat (' ', $aDir -> getDepth ());
- if ($aDir -> isDir ()) {
- $sHtml.= '<strong>'.$aDir -> current ().'</strong>';
- } else {
- $sHtml.= '<em>'.$aDir -> current ().' '.round (($aDir -> getSize()/1024), 2).' Ko</em>';
- }
- $sHtml .= '<br />';
- echo $sHtml;
- $aDir -> next ();
- }
-
- echo '<br /><strong>ALL</strong><br />';
- $aDir = $oDir -> getDir ();
- foreach ($aDir as $sK => $sV) {
- echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
- }
-
- echo '<br /><strong>FILTRE ON SUR PHP</strong><br />';
- $oDir -> FILTER_ON = 'php'; // only shows files with "php" in them
- $aDir = $oDir -> getDir ();
- foreach ($aDir as $sK => $sV) {
- echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
- }
-
- echo '<br /><strong>DIR = FALSE</strong><br />';
- $oDir -> DIR = false; // No directory
- $oDir -> FILTER_ON = null; // no filter ON
- $aDir = $oDir -> getDir ();
- foreach ($aDir as $sK => $sV) {
- echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
- }
-
- echo '<br /><strong>FILE = FALSE</strong><br />';
- $oDir -> DIR = true; // show directories
- $oDir -> FILE = false; // No files
- $aDir = $oDir -> getDir ();
- foreach ($aDir as $sK => $sV) {
- echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
- }
-
- echo '<br /><strong>PATH = bla ET FILTRE OFF SUR PHP</strong><br />';
- $oDir -> PATH = 'bla'; // parth = 'bla'
- $oDir -> FILE = true; // show files
- $oDir -> FILTER_OFF = 'php'; // do not show files with "php" in them
- $aDir = $oDir -> getDir ();
- foreach ($aDir as $sK => $sV) {
- //echo $sK, ' => ', $sV, '<br />';
- echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
- }
-
- /**
- * Create a copy of the whole directory recursively, but ONLY copy files with php in them ;-)
- */
- $oDir -> FILTER_ON = 'php';
- $oDir -> PATH = '.';
- $oDir -> copy ('copie');
-
- } catch (Exception $e) {
- echo $e -> getMessage (), ' => ', $e -> getLine ();
- }
- ?>
<?php
/**
* smartDir package
* @author Johan Barbier <johan.barbier@gmail.com>
* @version 20011120
*/
/**
* class myFilter
* check active filters
* RecursiveFilterIterator child
*/
class myFilter extends RecursiveFilterIterator {
/**
* active filter ON (keep these)
*
* @var mixed
*/
private $mFilterOn;
/**
* active filter OFF (do not keep these)
*
* @var mixed
*/
private $mFilterOff;
/**
* iterator
*
* @var iterator
*/
private $it = null;
/**
* error message
*
* @var string
*/
const ERROR_NO_VALID_FILTER = '{__FILTER__} is not a valid setable filter';
/**
* Constructor
*
* @param iterator $it
* @param mixed $mFilterOn
* @param mixed $mFilterOff
*/
public function __construct ($it, $mFilterOn, $mFilterOff) {
parent::__construct ($it);
$this -> it = $it;
$this -> mFilterOn = $mFilterOn;
$this -> mFilterOff = $mFilterOff;
}
/**
* Function accept ()
* returns true or false if the current element is accepted or not
*
* @return boolean
*/
public function accept () {
if (!is_null ($this -> mFilterOn)) {
if (!is_array ($this -> mFilterOn)) {
$mPos = strpos ($this -> it -> getFileName (), (string)$this -> mFilterOn);
if (false === $mPos) {
return false;
}
} else {
foreach ($this -> mFilterOn as $sFilter) {
$mPos = strpos ($this -> it -> getFileName (), (string)$sFilter);
if (false !== $mPos) {
return true;
}
}
return false;
}
}
if (!is_null ($this -> mFilterOff)) {
if (!is_array ($this -> mFilterOff)) {
$mPos = strpos ($this -> it -> getFileName (), (string)$this -> mFilterOff);
if (false !== $mPos) {
return false;
}
} else {
foreach ($this -> mFilterOff as $sFilter) {
$mPos = strpos ($this -> it -> getFileName (), (string)$sFilter);
if (false !== $mPos) {
return false;
} else {
return true;
}
}
return false;
}
}
return true;
}
}
/**
* Class MyRecursiveDirectoryIterator
* parent of main class, RecursiveDirectoryIterator child
*
*/
class MyRecursiveDirectoryIterator extends RecursiveDirectoryIterator {
/**
* are dir valid
*
* @var boolean
*/
protected $bDir = true;
/**
* are files valid
*
* @var boolean
*/
protected $bFile = true;
/**
* filter ON (keep these)
*
* @var mixed
*/
protected $mFilterOn = null;
/**
* filter OFF (do not keep these)
*
* @var mixed
*/
protected $mFilterOff = null;
/**
* Recursive dir or not
*
* @var boolean
*/
protected $bRecurse = true;
/**
* props that can be set
*
* @var array
*/
protected $aCanBeSet = array (
'FLAG',
'FILTER_ON',
'FILTER_OFF',
'DIR',
'FILE',
'PATH',
'RECURSE'
);
/**
* filter ON (keep these)
*
* @var RecursiveDirectoryIterator class constant
*/
protected $cFlag = 0;
/**
* path to be read
*
* @var string (valid path)
*/
protected $sPath;
protected $sCreatedDir = null;
/**
* error messages
*
* @var string
*/
const ERROR_PATH_NOT_FOUND = '{__PATH__} has not been found';
const ERROR_PROP_NOT_SETABLE = '{__PROP__} is not a setable property';
const ERROR_BAD_PROP_VALUE = '{__VAL__} is not a correct value for {__PROP__}';
const ERROR_NO_BOOLEAN = '{__PROP__} value must be a boolean';
/**
* getChildren will retrieve sub path
*
* @return subiterator
*/
public function getChildren () {
$iSub = new self ($this -> getPathname ());
$iSub -> bDir = $this -> bDir;
$iSub -> bFile = $this -> bFile;
$iSub -> mFilterOn = $this -> mFilterOn;
$iSub -> mFilterOff = $this -> mFilterOff;
return $iSub;
}
/**
* get current key
*
* @return string
*/
public function key () {
return $this -> getPath ();
}
/**
* get current file
*
* @return string
*/
public function current () {
return $this -> getFileName ();
}
/**
* is the current element valid or not
*
* @return boolean
*/
public function valid () {
if (!is_null ($this -> sCreatedDir)) {
if ($this -> current () === $this -> sCreatedDir) {
return false;
}
}
$oFilter = new myFilter ($this, $this -> mFilterOn, $this -> mFilterOff);
if (true === parent::valid ()) {
if (false === $oFilter -> accept ()) {
if (false === $this -> isDir ()) {
parent::next ();
return $this -> valid ();
} else {
return true;
}
}
if (false === $this -> bDir) {
if (true === $this -> isDir ()) {
parent::next ();
return $this -> valid ();
}
}
if (false === $this -> bFile) {
if (true === $this -> isFile ()) {
parent::next ();
return $this -> valid ();
}
}
return true;
}
return false;
}
/**
* Setter
*
* @param string $sProp
* @param mixed $mVal
*/
public function __set ($sProp, $mVal) {
if (!in_array ($sProp, $this -> aCanBeSet)) {
throw new Exception (str_replace ('{__PATH__}', $sProp, self::ERROR_PROP_NOT_SETABLE));
}
switch ($sProp) {
case 'FLAG' :
if (!in_array ($mVal, array (parent::CURRENT_AS_FILEINFO, parent::KEY_AS_FILENAME, parent::NEW_CURRENT_AND_KEY, 0))) {
throw new Exception (str_replace (array ('{__VAL__}', '{__PROP__}'), array ($mVal, $sProp), self::ERROR_PATH_NOT_FOUND));
}
$this -> cFlag = $mVal;
break;
case 'FILTER_ON' :
$this -> mFilterOn = $mVal;
$this -> mFilterOff = null;
break;
case 'FILTER_OFF' :
$this -> mFilterOff = $mVal;
$this -> mFilterOn = null;
break;
case 'DIR' :
if (!is_bool ($mVal)) {
throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_NO_BOOLEAN));
}
$this -> bDir = $mVal;
break;
case 'FILE' :
if (!is_bool ($mVal)) {
throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_NO_BOOLEAN));
}
$this -> bFile = $mVal;
break;
case 'RECURSE' :
if (!is_bool ($mVal)) {
throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_NO_BOOLEAN));
}
$this -> bRecurse = $mVal;
break;
case 'PATH' :
if (!is_dir ($mVal)) {
throw new Exception (str_replace ('{__PATH__}', $mVal, self::ERROR_PATH_NOT_FOUND));
}
$this -> sPath = $mVal;
break;
}
}
}
/**
* Class oSmartDir
* MyRecursiveDirectoryIterator child
*
*/
class oSmartDir extends MyRecursiveDirectoryIterator {
/**
* Constructor
*
* @param string $sPath (valid path)
*/
const ERROR_COPY_FAILED = 'Failed to copy {__FROM__} to {__TO__}';
public function __construct ($sPath) {
if (!is_dir ($sPath)) {
throw new Exception (str_replace ('{__PATH__}', $sPath, self::ERROR_PATH_NOT_FOUND));
}
$this -> sPath = $sPath;
}
/**
* getDir will retrieve the asked directory
*
* @return iterator
*/
public function getDir () {
parent::__construct ($this -> sPath, $this -> cFlag);
if (true === $this -> bRecurse) {
return new RecursiveIteratorIterator ($this, true);
} else {
return $this;
}
}
public function copy ($sTo) {
$aDir = $this -> getDir ();
if (!is_dir ($sTo)) {
mkdir ($sTo, '0755');
}
$this -> sCreatedDir = $sTo;
while ($aDir -> valid ()) {
if ($aDir -> current () !== $sTo && !$aDir -> isDot ()) {
if (!$aDir -> isDir ()) {
if (!@copy ($aDir -> getPathName (), $sTo.'/'.$aDir -> getPathName ())) {
throw new Exception (str_replace (array ('{__FROM__}', '{__TO__}'), array ($aDir -> getPathName (), $sTo.'/'.$aDir -> getPathName ()), self::ERROR_COPY_FAILED));
}
} else {
if (!@mkdir ($sTo.'/'.$aDir -> getPathName (), '0755')) {
throw new Exception (str_replace (array ('{__FROM__}', '{__TO__}'), array ($aDir -> getPathName (), $sTo.'/'.$aDir -> getPathName ()), self::ERROR_COPY_FAILED));
}
}
}
$aDir -> next ();
}
$this -> sCreatedDir = null;
}
}
/**
* STARTING EXAMPLES
*/
try {
$oDir = new oSmartDir ('.');
} catch (Exception $e) {
echo $e -> getMessage ();
}
try {
echo '<br /><strong>ALL NO RECURSIVE</strong><br />';
$oDir -> RECURSE = false; // no recursivity
$aDir = $oDir -> getDir ();
while ($aDir -> valid ()) {
if ($aDir -> isDot ()) {
$aDir -> next ();
}
$sHtml = '';
if ($aDir -> isDir ()) {
$sHtml.= '<strong>'.$aDir -> current ().'</strong>';
} else {
$sHtml.= '<em>'.$aDir -> current ().'</em>';
}
$sHtml .= '<br />';
echo $sHtml;
$aDir -> next ();
}
echo '<br /><strong>ALL</strong><br />';
$oDir -> RECURSE = true; // recursivity back to true
$aDir = $oDir -> getDir ();
while ($aDir -> valid ()) {
if ($aDir -> isDot ()) {
$aDir -> next ();
}
$sHtml = str_repeat (' ', $aDir -> getDepth ());
if ($aDir -> isDir ()) {
$sHtml.= '<strong>'.$aDir -> current ().'</strong>';
} else {
$sHtml.= '<em>'.$aDir -> current ().'</em>';
}
$sHtml .= '<br />';
echo $sHtml;
$aDir -> next ();
}
echo '<br /><strong>ALL + fileSize</strong><br />';
$oDir -> RECURSE = true; // recursivity back to true
$aDir = $oDir -> getDir ();
while ($aDir -> valid ()) {
if ($aDir -> isDot ()) {
$aDir -> next ();
}
$sHtml = str_repeat (' ', $aDir -> getDepth ());
if ($aDir -> isDir ()) {
$sHtml.= '<strong>'.$aDir -> current ().'</strong>';
} else {
$sHtml.= '<em>'.$aDir -> current ().' '.round (($aDir -> getSize()/1024), 2).' Ko</em>';
}
$sHtml .= '<br />';
echo $sHtml;
$aDir -> next ();
}
echo '<br /><strong>ALL</strong><br />';
$aDir = $oDir -> getDir ();
foreach ($aDir as $sK => $sV) {
echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
}
echo '<br /><strong>FILTRE ON SUR PHP</strong><br />';
$oDir -> FILTER_ON = 'php'; // only shows files with "php" in them
$aDir = $oDir -> getDir ();
foreach ($aDir as $sK => $sV) {
echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
}
echo '<br /><strong>DIR = FALSE</strong><br />';
$oDir -> DIR = false; // No directory
$oDir -> FILTER_ON = null; // no filter ON
$aDir = $oDir -> getDir ();
foreach ($aDir as $sK => $sV) {
echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
}
echo '<br /><strong>FILE = FALSE</strong><br />';
$oDir -> DIR = true; // show directories
$oDir -> FILE = false; // No files
$aDir = $oDir -> getDir ();
foreach ($aDir as $sK => $sV) {
echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
}
echo '<br /><strong>PATH = bla ET FILTRE OFF SUR PHP</strong><br />';
$oDir -> PATH = 'bla'; // parth = 'bla'
$oDir -> FILE = true; // show files
$oDir -> FILTER_OFF = 'php'; // do not show files with "php" in them
$aDir = $oDir -> getDir ();
foreach ($aDir as $sK => $sV) {
//echo $sK, ' => ', $sV, '<br />';
echo str_repeat ('-----', $aDir -> getDepth()), $sK, ' => ', $sV, '<br />';
}
/**
* Create a copy of the whole directory recursively, but ONLY copy files with php in them ;-)
*/
$oDir -> FILTER_ON = 'php';
$oDir -> PATH = '.';
$oDir -> copy ('copie');
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
?>
Historique
- 20 novembre 2006 16:21:41 :
- Ajout d'une méthode :
oSmartDir::copy (string sToDir)
Effectue une copie de l'arborescence courante vers le répertoire sToDir.
En pratique : cette copie prend en compte les filtres... ;-)
Voir le descriptif du package pour plus d'infos, et le fichier exemple (class.oSmartDir.php)
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Lecture de fichier [ par koko ]
Je voudrais lire un fichier fichier.txt dans la page index.phpvoici ma fonction<? echo "Texte: ". $fp = fopen("fichier.txt","r"); $donnees = fgets
changer les propriétés d'un fichier [ par PierScher ]
j'ai posté sur le ftp de free une police arial.ttf. A ce fichier, il ya plusieurs groupes Proprio, Utilisateurs et Tous. Par défaut, les attributs son
Lecture d'un dossier... [ par RockmanX ]
Voila mon "problème":Dans le dossier ci-dessous, il y a des images nommées:smile1.gif,smile2.gif,...smile8.gifj''ai écrit le script ci-dessous mais au
Fichier INDEX auto-exécutable [ par BSide ]
BSideBonjour,j'utilise EasyPHP1.6.Habituellement, quand je veux exécuter un script PHP, je vais sur le web local, je sélectionne le répertoire qui m'i
lecture et affichage de fichiers word [ par dolu007 ]
je dois réaliser un moteur de recherche et lorsque j'affiche la ligne avec le mot cherché les accents et caractère spéciaux sont écris n'importe comme
sous-repertoire fichier ............ [ par TRASH52 ]
TRASH52bon je vous met le code!En fait, je voudrais que mon programme a partir d'u
lecture de fichier [ par saad123 ]
bonjourje suis en train de faire un petit projet VB je voudrais lire le contenu d'un fichier puis l'afficher dans une text box. le truc c kil m'affich
ftp_get php [ par CC24 ]
bonjour à tous !est-ce que l'un d'entre vous pourrait m'indiquer la syntaxe à utiliser pour télécharger un fichier situé dans un sous-répertoire du ré
Lecture des dossier et sous-dossier.... [ par meridius ]
Hello tout le monde,Voilà j'aimerais parcourir tout mes dossiers et sous-dossiers pour trouver le fichier le plus récent et en récupérer la date pour
Rechercher capacité d'un répertoire sur un serveur [ par twiems ]
TwiemsJe souhaite rechercher et afficher la capacité d'un répertoire sur le serveur auquel mon pc est connecté.Y a t il une fonction ou une procédure
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|