RecRmDir and RecTimeDirRemove use *Iterator

This commit is contained in:
djmaze 2021-12-15 19:03:10 +01:00
parent b95541892f
commit bd7047bfae

View file

@ -888,22 +888,17 @@ abstract class Utils
{ {
if (\is_dir($sDir)) if (\is_dir($sDir))
{ {
$aObjects = \scandir($sDir); $iterator = new \RecursiveIteratorIterator(
foreach ($aObjects as $sObject) new \RecursiveDirectoryIterator($sDir, \FilesystemIterator::SKIP_DOTS),
{ \RecursiveIteratorIterator::CHILD_FIRST);
if ('.' !== $sObject && '..' !== $sObject) foreach ($iterator as $path) {
{ if ($path->isDir()) {
// if ('dir' === \filetype($sDir.'/'.$sObject)) \rmdir($path);
if (\is_dir($sDir.'/'.$sObject)) } else {
{ \unlink($path);
self::RecRmDir($sDir.'/'.$sObject);
}
else
{
\unlink($sDir.'/'.$sObject);
}
} }
} }
\clearstatcache();
return \rmdir($sDir); return \rmdir($sDir);
} }
@ -913,87 +908,20 @@ abstract class Utils
public static function RecTimeDirRemove(string $sTempPath, int $iTime2Kill, int $iNow = 0) : bool public static function RecTimeDirRemove(string $sTempPath, int $iTime2Kill, int $iNow = 0) : bool
{ {
$iNow = $iNow ?: \time(); $iTime = ($iNow ?: \time()) - $iTime2Kill;
$iFileCount = 0; \clearstatcache();
$iterator = new \RecursiveIteratorIterator(
$sTempPath = \rtrim($sTempPath, '\\/'); new \RecursiveDirectoryIterator($sTempPath, \FilesystemIterator::SKIP_DOTS),
if (\is_dir($sTempPath)) \RecursiveIteratorIterator::CHILD_FIRST);
{ foreach ($iterator as $path) {
$rDirH = \opendir($sTempPath); if ($path->isFile() && '.' !== $path->getBasename()[0] && $path->getMTime() < $iTime) {
if ($rDirH) \unlink($path);
{
$bRemoveAllDirs = true;
while (($sFile = \readdir($rDirH)) !== false)
{
if ('.' !== $sFile && '..' !== $sFile)
{
if (\is_dir($sTempPath.'/'.$sFile))
{
if (!static::RecTimeDirRemove($sTempPath.'/'.$sFile, $iTime2Kill, $iNow))
{
$bRemoveAllDirs = false;
}
}
else
{
$iFileCount++;
}
}
}
\closedir($rDirH);
} }
if ($iFileCount > 0)
{
if (static::TimeFilesRemove($sTempPath, $iTime2Kill, $iNow))
{
return \rmdir($sTempPath);
}
}
else
{
return $bRemoveAllDirs ? \rmdir($sTempPath) : false;
}
return false;
} }
\clearstatcache();
return true; return true;
} }
public static function TimeFilesRemove(string $sTempPath, int $iTime2Kill, int $iNow)
{
$bResult = true;
$sTempPath = \rtrim($sTempPath, '\\/');
if (\is_dir($sTempPath))
{
$rDirH = \opendir($sTempPath);
if ($rDirH)
{
while (($sFile = \readdir($rDirH)) !== false)
{
if ($sFile !== '.' && $sFile !== '..')
{
if ($iNow - \filemtime($sTempPath.'/'.$sFile) > $iTime2Kill)
{
\unlink($sTempPath.'/'.$sFile);
}
else
{
$bResult = false;
}
}
}
\closedir($rDirH);
}
}
return $bResult;
}
public static function Utf8Truncate(string $sUtfString, int $iLength) : string public static function Utf8Truncate(string $sUtfString, int $iLength) : string
{ {
if (\strlen($sUtfString) <= $iLength) if (\strlen($sUtfString) <= $iLength)