How can I get sub-directory names? How can I get the list of the folders inside public:// (or sites/default/files) location?

I have to show all the folder names to users, so that they can choose the subdirectory where they prefer to upload their documents.

I tried with the following code, but it returns the path together the folder name.

$directories = glob($somePath . '/*' , GLOB_ONLYDIR);


The value returned from my code is similar to the following one.

    [0] =&gt; sites/default/files//folder_name
    [1] =&gt; sites/default/files//new1
    [2] =&gt; sites/default/files//newfolder
You can ready the directory using read() function and drupal_realpath('public://') will give you the actual path to the public files folder.

$path = drupal_realpath('public://');
$dirs = array();
// directory handle
$dir = dir($path);

while (false !== ($entry = $dir-&gt;read())) {
    if ($entry != '.' &amp;&amp; $entry != '..') {
       if (is_dir($path . '/' .$entry)) {
            $dirs[] = $entry; 
       }
    }
}

echo '&lt;pre&gt;';print_r($dirs);echo '&lt;/pre&gt;';