Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder fireblade's Avatar
    Join Date
    May 2007
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    is_dir() probLem....

    Hi folks

    I'm having a bit of trouble with is_dir() in PHP 4.1.2. I want to
    create a menu dynamically from the names of folders in a web directory,
    and I'm using this code to create an array of the folders:

    PHP Code:
    function getDirContents($whichdir)
       {

       
    // directories to ignore
       
    $exclusions[] = ".";
       
    $exclusions[] = "..";
       
    $exclusions[] = "includes";
       
    $exclusions[] = "images";

       
    $handle opendir($whichdir);
       while(
    $dir readdir($handle))
           {
           if ((
    is_dir($dir)) && (!(in_array($dir$exclusions))))
               {
               
    $myDirList[] = $dir;
               }
           }
       
    closedir($handle);
       return 
    $myDirList;

       } 

    It works just fine if the function is called from a script where
    $whichdir is the current working directory or a sub-directory in the
    script's path, but not when $whichdir is 'up' a directory. I've tried
    using relative and absolute paths.

    The error is a peculiar one - the opendir() and readdir() seem to work,
    but the array returned is empty. If I exclude the is_dir() check, I get
    a full list of directories and files.

    I've RTFM and can't find anything about is_dir() behaviour when
    operating on a higher-level directory.

    Anyone run into this one?

    Thanks in advance
    Fi®e bLádê
    follow me on twitter

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You need to prepend the $whichdir directory to $dir when using is_dir() in this way.
    PHP Code:
    function getDirContents($whichdir)
    {
        
    // directories to ignore
        
    $exclusions[] = ".";
        
    $exclusions[] = "..";
        
    $exclusions[] = "includes";
        
    $exclusions[] = "images";

        
    $handle opendir($whichdir);
        while(
    $dir readdir($handle))
        {
            if ((
    is_dir($whichdir $dir)) && (!(in_array($dir$exclusions))))
            {
                
    $myDirList[] = $dir;
            }
        }
        
    closedir($handle);
        return 
    $myDirList;

    Make sure the directory you pass to the function has a trailing slash.


  •  

    Posting Permissions

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