Get a list of folders inside uploads directory

The closest is the WP Filesystem API, but that’s mainly intended for compatibility purposes, e.g. to enable files to be written when the only access is via FTP and direct filesystem write access is unavailable.

Particularly: https://developer.wordpress.org/reference/classes/wp_filesystem_base/dirlist/

However, if you are not distributing this code and only using it for yourself or known environments, you can use standard PHP APIs.

Note that you will need to perform some recursion to achieve this task, and there are costs to doing this, especially if the filesystem specified for WordPress involves remote requests such as FTP/S3/etc