SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to list folder names?

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

    I know that what I'm trying is trivial but I just can't do it!

    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:
    Code:
    <ul>
          <li><a href="?d=date_2008_10_05">date_2008_10_05</a></li>
          ....and so on
    </ul>

    can anyone help, please?


    TIA
    Proud member of this community.
    ____________________________

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    print_r(glob('foo/bar/date_*'GLOB_ONLYDIR)); 

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    Proud member of this community.
    ____________________________

  4. #4
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    take away the print_r and its an array
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats what it does...
    You should read the documentation for those functions. You can find documentation at www.php.net

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is what I have so far:
    Code PHP:
    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...
    Proud member of this community.
    ____________________________

  7. #7
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So change your code with malibus which works fine.
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you mean like this?
    Code PHP:
    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?
    Proud member of this community.
    ____________________________

  9. #9
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, like:
    PHP Code:
    $dirs glob(YOUR FOLDER NAMEGLOB_ONLYDIR);
     
    echo 
    '<ul>';
    foreach(
    $dirs as $dir)
    {
        echo 
    '<li><a href="?d='.$dir.'">'.$dir.'</a></li>';
    }
    echo 
    '</ul>'
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  10. #10
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where should I be looking for any errors if your code doesn't show anything? (I just tested it and nothing came up )

    the base_dir constant is decalred like this:
    Code PHP:
    $base_upload_dir = $_SERVER['DOCUMENT_ROOT'].'/blog/uploads/';
    define( 'BASE_UPLOAD_DIR', $base_upload_dir);
    Proud member of this community.
    ____________________________

  11. #11
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried removing the "date_*" part, like this
    Code:
    $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?
    Proud member of this community.
    ____________________________

  12. #12
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by haiku View Post
    I tried removing the "date_*" part, like this
    Code:
    $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?
    Just type out your full directory name manually to get it working.

    PHP Code:
    $dirs glob('/TYPE/THIS/OUT/date_*'GLOB_ONLYDIR);
    print_r($dirs); 
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've just "hacked" < lol a recursive function and it seems to work:

    Code PHP:
    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!
    Proud member of this community.
    ____________________________

  14. #14
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    Just type out your full directory name manually to get it working.

    PHP Code:
    $dirs glob('/TYPE/THIS/OUT/date_*'GLOB_ONLYDIR);
    print_r($dirs); 
    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!
    Proud member of this community.
    ____________________________

  15. #15
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by haiku View Post
    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.

  16. #16
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aahh!

    well, I tried different variations but this one worked:
    Code PHP:
            $dir = BASE_UPLOAD_DIR;
            $dirs = glob($dir . '/*', GLOB_ONLYDIR);

    When I passed the BASE_UPLOAD_DIR directly, like this, nothing was returned:
    Code PHP:
            $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
    Proud member of this community.
    ____________________________


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •