Recursively unlinks a directory

@param string $dir The target directory that will be removed
@param bool TRUE on success or FALSE on failure
13607
public static function recursiveUnlink($dir) {
        $result = TRUE;

        if(is_dir($dir)) {
            foreach(scandir($dir) as $sub) {
                if(in_array($sub, ['.', '..'])) continue;
                $result &= self::recursiveUnlink("$dir/$sub");
            }

            $result &= rmdir($dir);
        }
        elseif(is_file($dir)) {
            $result &= unlink($dir);
        }
        else {
            $result = FALSE;
        }

        return (bool)$result;
    }
738626
public function unshiftResourceDir($dir)
    {
        File::resolveFilepath($dir);

        $resourcedir = $this->getResourceDir();
        array_unshift($resourcedir, $dir);

        $this->resourceDir = $resourcedir;

        return $this;
    }
788392
public static function rmdirRecursive(string $directory): void
    {
        if (is_dir($directory)) {
            static::emptyDirectory($directory);

            if (!rmdir($directory)) {
                throw new FilesystemException("Unable to remove directory on path={$directory}");
            }
        }
    }
1280902
public static function delDir($dir) {
        $all = self::readDir($dir, self::ALL, true, NULL);
        if (isset($all['files'])) {
            foreach ($all['files'] as $file) {
                if (is_array($file)) {
                    foreach ($file as $fil) {
                        if (!unlink($fil)) {
                            return false;
                        }
                    }
                } else {
                    if (!unlink($file)) {
                        return false;
                    }
                }
            }
        }

        if (isset($all['dirs'])) {
            foreach (array_reverse($all['dirs']) as $_dir) {
                if (is_array($_dir)) {
                    foreach ($_dir as $_dr) {
                        if (!rmdir($_dr)) {
                            return false;
                        }
                    }
                } else {
                    if (!rmdir($_dir)) {
                        return false;
                    }
                }
            }
        }

        return rmdir($dir);
    }
280087
protected function _rmdir( $dir )
    {
        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator( $dir,
                RecursiveDirectoryIterator::CURRENT_AS_FILEINFO |
                RecursiveDirectoryIterator::SKIP_DOTS
            ),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        $result = true;

        foreach ( $iterator as $info )
        {
            $path = $info->getPathname();

            switch ( true )
            {
                case $info->isFile():
                case $info->isLink():
                    $result = $result && @ unlink( $path );
                    break;

                case $info->isDir():
                    $result = $result && @ rmdir( $path );
                    break;
            }

        }

        return $result && @ rmdir( $dir );
    }
244809
public static function removeDir($dir)
    {
        if (is_dir($dir)) {
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ('.' != $object && '..' != $object) {
                    if ('dir' == filetype($dir . DS . $object)) {
                        static::removeDir($dir . DS . $object);
                    } else {
                        if (! unlink($dir . DS . $object)) {
                            throw new ErrorException(
                                'The following file could not be deleted: ' . $dir . DS . $object
                            );
                        }
                    }
                }
            }

            if (! rmdir($dir)) {
                throw new ErrorException("The following directory could not be deleted: {$dir}");
            }
        }
    }
616977
public static function rmdir( $dir ) {
		$files = new RecursiveIteratorIterator(
			new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ),
			RecursiveIteratorIterator::CHILD_FIRST
		);

		foreach ( $files as $fileinfo ) {
			$todo = $fileinfo->isDir() ? 'rmdir' : 'unlink';
			$todo( $fileinfo->getRealPath() );
		}
		rmdir( $dir );
	}
704669
protected function deleteTree($directory, $removeBaseDir = false)
    {
        $files = array_diff(scandir($directory), array('.', '..'));
        foreach ($files as $file) {
            (is_dir("$directory/$file")) ? $this->deleteTree("$directory/$file", true) : @unlink("$directory/$file");
        }

        if ($removeBaseDir) {
            @rmdir($directory);
        }
    }
648784
public static function isLink(string $dir = ''): bool
    {
        $dir = static::normalizePath($dir);
        return static::exists($dir) && \is_link($dir);
    }
35080
private function removeTemporaryDirectory($dir, $rmBaseDir = false)
    {
        $itemsToIgnore = array('.', '..', '.htaccess');

        $files = array_diff(scandir($dir), $itemsToIgnore);
        foreach ($files as $file) {
            if (is_dir("$dir/$file")) {
                $this->removeTemporaryDirectory(
                    "$dir/$file",
                    $file == 'smarty' ? $rmBaseDir : true
                );
            } else {
                @unlink("$dir/$file");
            }
        }
        if ($rmBaseDir) {
            @rmdir($dir);
        }
    }
1035765
private function rmdir($dir, $recursive)
	{
		$is_link = is_link($dir);
		if($is_link)
		{
			if(! unlink($dir))
				throw new Exception("Error unlinking $dir");
		}
		else if(! $recursive)
		{
			if(! rmdir($dir))
				throw new Exception("Error removing temp dir: $dir");
		}
		else
		{
			$dh = opendir($dir);
			if(! $dh)
				throw new Exception("Cannot read temp dir contents for removal: $dir");

			do
			{
				$file = readdir($dh);
				if($file === false)
					break;

				// Don't delete current dir or parent dir (yet)
				if($file === '.' || $file === '..')
					continue;

				$path = $dir .DIRECTORY_SEPARATOR. $file;

				$is_link = is_link($path);
				$is_dir = is_dir($path);

				if($is_dir && ! $is_link)
				{
					$this->rmdir($path, true);
				}
				else // anything else, should be able to unlink
				{
					if(! unlink($path))
						throw new Exception("Cannot remove nested temp file: $path");
				}
			}
			while($file !== false);

			closedir($dh);

			// Now remove the dir itself (non-recursive, it should now be empty)
			$this->rmdir($dir, false);
		}
	}
121616
public static function deleteFolder($dir)
    {
        $dh = @opendir($dir);
        if (!$dh) {
            return false;
        }
        while (false !== ($file = readdir($dh))) {
            if (($file != '.') && ($file != '..')) {
                $full = $dir . '/' . $file;
                if (is_dir($full)) {
                    self::deleteFolder($full);
                } else {
                    unlink($full);
                }
            }
        }
        closedir($dh);
        rmdir($dir);
        return true;
    }
200016
protected function clearDir($dir)
    {
        $iterator = new \DirectoryIterator($dir);
        foreach ($iterator as $file) {

            if ($file->isDir() && !$file->isDot() && !preg_match("~\.~", $file)) {
                $this->clearDir($file->getPathname());
                if (!\rmdir($file->getPathname())) {
                    return false;
                }
            } elseif ($file->isFile()) {
                if (!\unlink($file->getPathname())) {
                    return false;
                }
            }
        }

        return true;
    }
946359
function recursiveRemoveDir($dir)
    {
        if (is_link($dir)) {
            @unlink($dir);
        } else if (is_dir($dir)) {
            $files = array_diff(@scandir($dir), array('.', '..'));
            foreach ($files as $file) {
                (is_dir("$dir/$file")) ? $this->recursiveRemoveDir("$dir/$file") : @unlink("$dir/$file");
            }
        }
        return @rmdir($dir);
    }
296806
protected function deleteDirectory($dir)
    {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileInfo) {
            $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');

            $todo($fileInfo->getRealPath());
        }

        rmdir($dir);
    }
410450
static public function rmdir($dir, $recursive = false)
    {
        $dir = realpath($dir);
        if (!is_dir($dir)) return true;
        if (true !== $recursive) return @rmdir($dir);
        $dp = @opendir($dir);
        while ($file = @readdir($dp)) {
            if ('.' === $file || '..' === $file) continue;
            $cur_file = $dir . '/' . $file;
            if (is_dir($cur_file)) {
                self::rmdir($cur_file, true);
            } elseif (is_file($cur_file)) {
                @unlink($cur_file);
            }
        }
        @closedir($dp);
        return @rmdir($dir);
    }
1005904
public static function truncate_directory ($dir)
    {
        if (substr($dir, strlen($dir)-1, 1) != '/') {
            $dir .= '/';
        }

        if (!$fileList = scandir ($dir)) {
            return false;
        }

        foreach ($fileList as $fileInList) {
            if (is_dir($dir . $fileInList) && $fileInList != '.' && $fileInList != '..') {
                // Some more directory. Recurse!
                if (!self::rmrf($dir . $fileInList . '/')) {
                    // If false, Error message was already given by rmrf.
                    return false;
                }
            } elseif (is_file($dir . $fileInList)) {
                if (!unlink ($dir . $fileInList)) {
                    return false;
                }
            }
        }

        return true;
    }