Jump to content


Photo

Creating an array of directories


  • Please log in to reply
6 replies to this topic

#1 jkies00

jkies00
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 September 2006 - 06:38 PM

Hello,

I'm trying to create an array of directories within a specific directory:

$myDirectory = opendir("."); //opens directory
while($entryName = readdir($myDirectory)) {
	if(strcmp(filetype($entryName),"dir")==0)){$dirArray[] = $entryName;}  // if the filetype is a directory, add it to the array
}

The script works if I take out the if statement, but lists files and folders alike.  I'd like to get a list of only the folders.  What am I doing wrong here?  Any opinions welcome, I'm a n00b.  :o)

~jk



#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 September 2006 - 06:45 PM

Try this.
$myDirectory = opendir("."); //opens directory
while($entryName = readdir($myDirectory)) {
     if (is_dir($entryName) == true) {
        //add it to your array
     }
}

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 jkies00

jkies00
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 September 2006 - 07:14 PM

Thanks, Tom - I tried that and it worked.

Also got it to work like this:

while($entryName = readdir($myDirectory)) {
	if(filetype($entryName)=="dir"){$dirArray[] = $entryName;}
}

...but I think your way is cleaner.  Thanks!!

#4 jkies00

jkies00
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 September 2006 - 08:53 PM

Hello,

I noticed something weird - I was able to succesfully create my array, but the first two values in the array are:
.
..

what are they?

#5 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 05 September 2006 - 08:59 PM

. your current dir
.. your 1-up dir

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#6 jkies00

jkies00
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 September 2006 - 09:03 PM

why would they be in my array?  i only allowed files of type dir.  does php create a dir for each of those that doesn't show in windows explorer view?

#7 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 September 2006 - 09:17 PM

Here is a function for creating an array of both files and directories. I think that this sinpett came from Jenk

<?php

function filelist($dir)
{
    if (!$dir = realpath($dir)) return null;

    static $files = array();
    static $dirs = array();

    $handle = opendir($dir);

    while (($file = readdir($handle)) !== false)
    {
        if (!in_array($file, array('.', '..')))
        {
            if (is_dir($path = ($dir . DIRECTORY_SEPARATOR . $file)))
            {
                $dirs[] = $path;
                filelist($path);
            } else {
                $files[] = $path;
            }
        }
    }
    
    return array('dirs' => $dirs, 'files' => $files);
}

echo nl2br(print_r(filelist('.'), true));

?>

This will create an array with the directories and files in it. You can modify it to suit your needs.

Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users