public static function unlinkRecursive($dir, $deleteRootToo, \Closure $beforeUnlink = null)
    {
        if (!$dh = @opendir($dir)) {
            return;
        }
        while (false !== ($obj = readdir($dh))) {
            if ($obj == '.' || $obj == '..') {
                continue;
            }

            $path = $dir . '/' . $obj;
            if ($beforeUnlink) {
                $beforeUnlink($path);
            }

            if (!@unlink($path)) {
                self::unlinkRecursive($path, true);
            }
        }
        closedir($dh);
        if ($deleteRootToo) {
            @rmdir($dir);
        }
        return;
    }
public static function unlinkRecursive($dir, $deleteRootToo, \Closure $beforeUnlink = null)
    {
        if (!$dh = @opendir($dir)) {
            return;
        }
        while (false !== ($obj = readdir($dh))) {
            if ($obj == '.' || $obj == '..') {
                continue;
            }

            $path = $dir . '/' . $obj;
            if ($beforeUnlink) {
                $beforeUnlink($path);
            }

            if (!@unlink($path)) {
                self::unlinkRecursive($path, true);
            }
        }
        closedir($dh);
        if ($deleteRootToo) {
            @rmdir($dir);
        }
        return;
    }
public static function unlinkRecursive ( $ dir , $ deleteRootToo , \ Closure $ beforeUnlink = null ) { if ( ! $ dh = @ opendir ( $ dir ) ) { return ; } while ( false !== ( $ obj = readdir ( $ dh ) ) ) { if ( $ obj == '.' || $ obj == '..' ) { continue ; } $ path = $ dir . '/' . $ obj ; if ( $ beforeUnlink ) { $ beforeUnlink ( $ path ) ; } if ( ! @ unlink ( $ path ) ) { self :: unlinkRecursive ( $ path , true ) ; } } closedir ( $ dh ) ; if ( $ deleteRootToo ) { @ rmdir ( $ dir ) ; } return ; }
Recursively deletes a directory.

@param string $dir Path of the directory to delete.
@param boolean $deleteRootToo If true, `$dir` is deleted, otherwise just its contents.
@param \Closure|false $beforeUnlink An optional closure to execute on a file path before unlinking.
@api
Recursively deletes a directory .