Clean a directory.

@param string $dir
The directory to clean.
@param int $keep
The number of items to keep.
545452
protected function cleanDir($dir, $keep)
    {
        $finder = clone $this->finder;
        $finder->in($dir);
        $finder->depth(0);
        $this->doSort($finder);
        $items = iterator_to_array($finder->getIterator());
        if ($keep) {
            array_splice($items, -$keep);
        }
        while ($items) {
            $item = reset($items);
            try {
                $this->fs->chmod($item, 0777, 0000, true);
            } catch (IOException $e) {
                // If chmod didn't work and the exception contains a path, try
                // to remove anyway.
                $path = $e->getPath();
                if ($path && realpath($path) !== realpath($item)) {
                    $this->fs->remove($path);
                    continue;
                }
            }
            $this->fs->remove($item);
            array_shift($items);
        }
    }
1271130
public function deleteDirectory( string $directory, $preserve = false )
	{
		if( !$this->isDirectory( $directory ) )
		{
			return false;
		}

		$items = new FilesystemIterator( $directory );
		foreach( $items as $item )
		{
			// If the item is a directory, we can just recurse into the function and
			// delete that sub-directory otherwise we'll just delete the file and
			// keep iterating through each file until the directory is cleaned.
			if( $item->isDir() && !$item->isLink() )
			{
				$this->deleteDirectory( $item->getPathname() );
			}

			// If the item is just a file, we can go ahead and delete it since we're
			// just looping through and waxing all of the files in this directory
			// and calling directories recursively, so we delete the real path.
			else
			{
				$this->deleteOnce( $item->getPathname() );
			}
		}

		if( ! $preserve )
		{
			@ rmdir( $directory );
		}

		return true;
	}
383835
public function ensureDirectory($directory)
	{
		if(!is_dir($directory))
		{
			$this->ensureDirectory(dirname($directory));
			echo "      mkdir ".strtr($directory,'\\','/')."\n";
			mkdir($directory);
		}
	}
620099
protected function deleteFile($file, $dir) {
        if ($file != "." && $file != "..") {
            if (is_dir($dir."/".$file)) {
                $this->flushDir($dir."/".$file);
                rmdir($dir."/".$file);
            } else {
                unlink($dir."/".$file);
            }
        }
    }
869456
function _remove_dir($dir)
    {
        $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $dir));

        $temp = &$this->dirs;
        foreach ($dirs as $dir) {
            if ($dir == end($dirs)) {
                unset($temp[$dir]);
                return true;
            }
            if (!isset($temp[$dir])) {
                return false;
            }
            $temp = &$temp[$dir];
        }
    }
209932
public static function dirList($dir) {
        if (!is_dir($dir))
            throw new \Exception('"' . $dir . '" is not a valid directory');

        $l = array();
        foreach (self::cleanScandir($dir) as $f) {
            if (is_dir($dir . $f)) {
                $l[] = $dir . $f . '/';
                $l = array_merge($l, self::dirList($dir . $f . DIRECTORY_SEPARATOR));
            }
        }
        return $l;
    }
544179
protected function flushDir($dir)
    {
        if (!file_exists($dir)) {
            echo "HELP cant find ".$dir."\n";
            return;
        }
        $objects = scandir($dir);
        foreach ($objects as $object) {
            try {
                $this->deleteFile($object, $dir);
            } catch (Exception $e) {
                echo "Can't remove Folder/File ".$dir.DIRECTORY_SEPARATOR.$object."<br />\n"
                        . $e->getMessage()."<br />\n";
            }
        }
    }
118744
public function setCacheDir($dir)
    {
        if (GenericValidator::validateDirName($dir)) {
            $this->cacheDir = $dir;
            FileSystemHelper::setUpDir($dir);
            return true;
        }
        return false;
    }
1118339
private void cleanDir(File dir) {
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                // if file is a directory, first delete all the files inside, then delete the directory itself.
                if (file.isDirectory()) {
                    cleanDir(file);
                }
                if (!file.delete()) {
                    throw new BuildException(MessageFormat.format(messages.getString("error.cannot.delete.file"), file.getAbsolutePath()));
                }
            }
        }
    }
537313
static function delete_empty_dir($dir)
    {
        $dir = self::makeVirtualDir($dir, false);
        if (is_dir($dir) && sizeof(scandir($dir)) == 2){
            return @rmdir($dir);
        }
        self::deleteVirtualDir($dir);
        return false;
    }
722406
protected function cleanUpDir($directory)
    {
        $this->filesystem->removeDirectory($directory);
        $this->filesystem->ensureDirectoryExists($directory);
        return $this;
    }
795805
private static function clearDir($dir, $rmSelf = false) 
    {
        $dir = rtrim($dir, '\\/') . '/';
        
        if(!$dir || !$d = dir($dir)) {
            return;
        }

        $do = true;
        while (false !== ($entry = @$d->read())) {
            if($entry[0] == '.') {
                continue;
            }
            
            $path = $dir.'/'.$entry;
            if (is_dir($path)) {
                $do = $do && static::clearDir($path, true);
            } else {
                $do = $do && false !== @unlink($path);
            }
        }
            
        @$d->close();
        $rmSelf && @rmdir($dir);
        
        return $do;
    }
355203
public function rmdirr($dir)
    {
        if ($handle = opendir("$dir")) {
            while ($item = readdir($handle)) {
                if (($item != ".") && ($item != "..")) {
                    if (is_dir("$dir/$item")) {
                        $this->rmdirr("$dir/$item");
                    } else {
                        unlink("$dir/$item");
                    }
                }
            }
            closedir($handle);
            rmdir($dir);
        }
    }
359698
public function cleanDir($dir)
    {
        if (empty($dir)) return false;

        $dir === 'all' && $dir = '';//删除所有
        $fullDir = $this->conf['CACHE_PATH'] . $dir;
        if (!is_dir($fullDir)) {
            return false;
        }

        $files = scandir($fullDir);
        foreach ($files as $file) {
            if ('.' === $file || '..' === $file) continue;
            $tmp = $fullDir . DIRECTORY_SEPARATOR . $file;
            if (is_dir($tmp)) {
                $this->cleanDir($dir . DIRECTORY_SEPARATOR . $file);
            } else {
                unlink($tmp);
            }
        }
        rmdir($fullDir);
        return true;
    }
1344571
protected function deleteFromDir(
        /*# string */ $dir,
        /*# int */ $maxlife = 0,
        /*# bool */ $removeDir = false
    )/*# : bool */ {
        $now = time();
        if (is_dir($dir)) {
            $files = scandir($dir);
            foreach ($files as $file) {
                if ($file == "." || $file == "..") {
                    continue;
                }
                $sub = $dir . DIRECTORY_SEPARATOR . $file;
                $res = true;
                if (is_dir($sub)) {
                    $res = $this->deleteFromDir($sub, $maxlife, true);
                } else {
                    if (!$maxlife || $now - filemtime($sub) > $maxlife) {
                        $res = unlink($sub);
                    }
                }
                if ($res === false) {
                    return $this->falseAndSetError(
                        Message::get(Message::CACHE_FAIL_DELETE, $sub),
                        Message::CACHE_FAIL_DELETE
                    );
                }
            }
            if ($removeDir && ! @rmdir($dir)) {
                return $this->falseAndSetError(
                    Message::get(Message::CACHE_FAIL_DELETE, $dir),
                    Message::CACHE_FAIL_DELETE
                );
            }
        }
        return true;
    }
355259
public function mkdir($dir) {
		$this->connectIfNeeded();
		$this->handle->mkdir($dir);
		$this->onFolderCreated(new Event(['sender' => $this, 'data' => $dir]));
	}
16707
private function _rrmdir($dir)
    {
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != '.' && $file != '..') {
                    if (is_dir($dir . '/' . $file)) {
                        $this->_rrmdir($dir . '/' . $file);
                        rmdir($dir . '/' . $file);
                    } else {
                        unlink($dir . '/' . $file);
                    }
                }
            }
            closedir($handle);
        }
    }