Простой PHP класс позволяющий перебрать все папки и файлы, например, для построения дерева.
Класс для рекурсивного обхода файловой системы.
21.02.2017
class.scan.php (Download)
<?
class soScanFS
{
/**
* Найдена директория
*
* @param string $sPath
*
* @return void
*/
protected function onDirFind($sPath)
{
echo 'Dir: '.$sPath."<br>\n";
}
/**
* Найден файл
*
* @param string $sPath
*
* @return void
*/
protected function onFileFind($sPath)
{
echo 'File: '.$sPath."<br>\n";
}
/**
* Сканируем директорию
*
* @param string $sPath
*
* @return void
*/
public function scan($sPath)
{
$d = dir($sPath);
while (false !== ($entry = $d->read()))
{
if ( ($entry != '.') && ($entry != '..') )
{
$sCurrPath = $sPath.'/'.$entry;
if (is_dir($sCurrPath))
{
$this->onDirFind($sCurrPath);
$this->scan($sCurrPath);
}
else
{
$this->onFileFind($sCurrPath);
}
}
}
$d->close();
}
}
$oScanFS = new soScanFS();
$oScanFS->scan( dirname(__DIR__) );
?>