How to list folder names?

The title pretty much says it all, but I’ve searched and tried many scripts but to no avail :frowning:

I know that what I’m trying is trivial but I just can’t do it! :frowning:

I have a folder, called “updates” and within it some files and other folders.
Some of these folders are prefixed with “date_” and these folders I’m interested in so I want to make a list out of them.

ex:


<ul>
      <li><a href="?d=date_2008_10_05">date_2008_10_05</a></li>
      ....and so on
</ul>

can anyone help, please?

TIA


print_r(glob('foo/bar/date_*', GLOB_ONLYDIR));

Thanks for your quick reply! but may I ask for a little more?

I’d like to receive the folder names as an array because I’m doing this in a function and then use a foreach to get them.

please? :slight_smile:

take away the print_r and its an array

Thats what it does…
You should read the documentation for those functions. You can find documentation at www.php.net

this is what I have so far:


function listFolders()
{
    // get the base directory
    $base_dir = dir(BASE_UPLOAD_DIR) or die("Failed opening the directory for reading");

    $base_dir = '"'.BASE_UPLOAD_DIR.'"';
    if (is_dir($base_dir))
    {
        while (($dir = readdir($base_dir)) !== FALSE)
        {
            if (is_dir($dir) /* && check if the folder contains "date_" */)
            {
                $dirs[] = array (
                    'name' => // get the folder name
                );
            }
        }
       closedir($base_dir);
    }
    return $dirs;
}

but this doesn’t work…

So change your code with malibus which works fine.

you mean like this?


function listFolders()
{
    // get the base directory
    $base_dir = dir(BASE_UPLOAD_DIR) or die("Failed opening the directory for reading");

    $base_dir = '"'.BASE_UPLOAD_DIR.'"';
    return glob($base_dir.'date_*', GLOB_ONLYDIR);
}



$dirs = listFolders();

    echo '<ul>';
    foreach($dirs as $dir)
    {
        echo '<li><a href="?d='.$dir.'">'.$dir.'</a></li>';
    }
    echo '</ul>';


it doesn’t return anything… what am I missing here? :frowning:

No, like:


$dirs = glob(YOUR FOLDER NAME, GLOB_ONLYDIR);
 
echo '<ul>';
foreach($dirs as $dir)
{
    echo '<li><a href="?d='.$dir.'">'.$dir.'</a></li>';
}
echo '</ul>';

where should I be looking for any errors if your code doesn’t show anything? (I just tested it and nothing came up :frowning: )

the base_dir constant is decalred like this:


$base_upload_dir = $_SERVER['DOCUMENT_ROOT'].'/blog/uploads/';
define( 'BASE_UPLOAD_DIR', $base_upload_dir);

I tried removing the “date_*” part, like this


$dirs = glob($base_dir, GLOB_ONLYDIR);
$dirs = glob($base_dir."*", GLOB_ONLYDIR);

and still it doesn’t return anything…

I run this in DW and I have two folders prefixed with date there…

I also have another script that returns all images from a selected folder which uses the $base_dir variable and it works…

why doesn’t this one? :frowning:

Just type out your full directory name manually to get it working.

$dirs = glob('/TYPE/THIS/OUT/date_*', GLOB_ONLYDIR);
print_r($dirs);

I’ve just “hacked” < lol a recursive function and it seems to work:


function listdirs($dir) {
    $dirs = glob($dir . '/*', GLOB_ONLYDIR);
    foreach ($dirs as $dir) {
        if (eregi("date_", $dir))
        {
            $alldirs[] = $dir; 
        }
    }
    return $alldirs;
}

$dirs = listdirs($base_upload_dir);
echo '<ul>';
foreach($dirs as $dir)
{
    echo '<li><a href="?d='.$dir.'">'.$dir.'</a></li>';
}
echo '</ul>';


Thanks for your help! (I’ll go study the glob function thoroughly a bit later) For now I’m happy it works. yay!

Merry Christmas, you two! :slight_smile:

I’m not happy with that approach because my files could change their location, this is the reason I made the base_dir global.

Thanks for your help!

He means, just type it out manually as a troubleshooting step to rule out the possibility of the global variable/constant being the problem.

aahh!

well, I tried different variations but this one worked:


        $dir = BASE_UPLOAD_DIR;
        $dirs = glob($dir . '/*', GLOB_ONLYDIR);

When I passed the BASE_UPLOAD_DIR directly, like this, nothing was returned:


        $dirs = glob(BASE_UPLOAD_DIR. '/*', GLOB_ONLYDIR);

I don’t know why that happened but there’s no one to ask, so I’ll live with it.

And when I changed the BASE_UPLOAD_DIR with its default path, it worked without problems.

Thanks for your input :slight_smile: