public static function remdir($path, $leave_folder = false)
    {
        if (!is_dir($path)) {
            return false;
        }

        // Always have trailing slash
        $slash = substr($path, -1);
        if ($slash != '/' && $slash != '\\') {
            $path .= '/';
        }

        // Remove all inner folders
        foreach (array_diff(scandir($path), ['.', '..']) as $v) {
            if (is_dir($path . $v)) {
                self::remdir($path . $v . '/', $leave_folder);
            } elseif (!is_writable($path . $v) || !unlink($path . $v)) {
                return false;
            }
        }

        // Skip latest folder if required
        if ((!$leave_folder) && !rmdir($path)) {
            return false;
        }

        return true;
    }
public static function remdir($path, $leave_folder = false)
    {
        if (!is_dir($path)) {
            return false;
        }

        // Always have trailing slash
        $slash = substr($path, -1);
        if ($slash != '/' && $slash != '\\') {
            $path .= '/';
        }

        // Remove all inner folders
        foreach (array_diff(scandir($path), ['.', '..']) as $v) {
            if (is_dir($path . $v)) {
                self::remdir($path . $v . '/', $leave_folder);
            } elseif (!is_writable($path . $v) || !unlink($path . $v)) {
                return false;
            }
        }

        // Skip latest folder if required
        if ((!$leave_folder) && !rmdir($path)) {
            return false;
        }

        return true;
    }
public static function remdir ( $ path , $ leave_folder = false ) { if ( ! is_dir ( $ path ) ) { return false ; } // Always have trailing slash $ slash = substr ( $ path , - 1 ) ; if ( $ slash != '/' && $ slash != '\\' ) { $ path .= '/' ; } // Remove all inner folders foreach ( array_diff ( scandir ( $ path ) , [ '.' , '..' ] ) as $ v ) { if ( is_dir ( $ path . $ v ) ) { self :: remdir ( $ path . $ v . '/' , $ leave_folder ) ; } elseif ( ! is_writable ( $ path . $ v ) || ! unlink ( $ path . $ v ) ) { return false ; } } // Skip latest folder if required if ( ( ! $ leave_folder ) && ! rmdir ( $ path ) ) { return false ; } return true ; }
Remove folder recursively with all content from file system
@param string $path
@param bool $leave_folder itself or delete it too
@return bool
Remove folder recursively with all content from file system