Getting the filenames from a folder

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krustov

    Getting the filenames from a folder

    <?php
    $dirname = ".";
    $dh = opendir($dirnam e);
    while ($file = readdir($dh))
    {
    if (is_dir ("$dirname/$file"))
    {
    print "";
    }
    print "$file<br>" ;
    }
    closedir ($dh);
    ?>


    ..
    ...
    aaa.html
    etc

    The above code brings up the single and twin full stops - presumably the
    option to change directorys or whatever if wanted .

    Got the code snippet via google and just wondered if the above code is
    the best way it could be written .
  • Andy Hassall

    #2
    Re: Getting the filenames from a folder

    On Sun, 30 Oct 2005 15:10:42 -0000, Krustov <krusty@krustov .co.uk.INVALID>
    wrote:
    [color=blue]
    ><?php
    >$dirname = ".";
    >$dh = opendir($dirnam e);
    >while ($file = readdir($dh))
    >{
    >if (is_dir ("$dirname/$file"))
    >{
    >print "";
    >}
    >print "$file<br>" ;
    >}
    >closedir ($dh);
    >?>
    >.
    >..
    >aaa.html
    >etc
    >
    >The above code brings up the single and twin full stops - presumably the
    >option to change directorys or whatever if wanted .
    >
    >Got the code snippet via google and just wondered if the above code is
    >the best way it could be written .[/color]

    See the example in the manual, it's superior in a couple of ways.



    For example, consider the behaviour of the code you posted in the presence of
    files or directories named "0".
    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Krustov

      #3
      Re: Getting the filenames from a folder

      <comp.lang.ph p , Andy Hassall , andy@andyh.co.u k>
      <tjp9m11cstulcd or7jfdji31vcva3 d0nff@4ax.com>
      <Sun, 30 Oct 2005 15:29:18 +0000>
      [color=blue][color=green]
      > >Got the code snippet via google and just wondered if the above code is
      > >the best way it could be written .[/color]
      >
      > See the example in the manual, it's superior in a couple of ways.
      >
      > http://uk.php.net/opendir
      >[/color]

      Fair enough - just thought I would ask before I started messing around
      with it .

      Comment

      • Chung Leong

        #4
        Re: Getting the filenames from a folder

        I usually use glob() 'cause I'm lazy.

        Find pathnames matching a pattern


        Comment

        • juglesh

          #5
          Re: Getting the filenames from a folder


          Chung Leong wrote:[color=blue]
          > I usually use glob() 'cause I'm lazy.
          >
          > http://fi.php.net/glob/[/color]

          nice, i never saw that. I assume you could go *.*, can you give it
          multiple matches, like *.jpg, *.gif ?

          here is a func which can give an array of either folders or files:

          $aListOfFiles = GetDirList($som eDirectory, 'files');
          $aListOfFfolder s = GetDirList($som eDirectory, 'folders');


          function GetDirList($par ent, $filesorfolders ){
          $oldDir = getcwd();
          $list = array();

          if ($handle = opendir($parent )) {
          chdir($parent);
          while (false !== ($file = readdir($handle ))) {

          if ($filesorfolder s == "folders"){
          if ( (is_dir($file)) && ($file != ".") && ($file !=
          ".."))
          {$list[] = $file;
          }}
          if ($filesorfolder s == "files"){
          if ( (is_file($file) ) && ($file != ".") && ($file !=
          ".."))
          {$list[] = $file;
          }}


          }// while reading
          closedir($handl e);
          chdir($oldDir);
          }//if handle

          sort($list);
          reset($list);
          return $list;
          } //func

          Comment

          • Chung Leong

            #6
            Re: Getting the filenames from a folder


            juglesh wrote:[color=blue]
            > Chung Leong wrote:[color=green]
            > > I usually use glob() 'cause I'm lazy.
            > >
            > > http://fi.php.net/glob/[/color]
            >
            > nice, i never saw that. I assume you could go *.*, can you give it
            > multiple matches, like *.jpg, *.gif ?[/color]

            Yes, if you pass the GLOB_BRACE flag.

            Example:

            $images = glob("*.{jpeg,j pg,gif,png}", GLOB_BRACE);

            You can also character class matching with glob():

            $images = glob("[ab]*.gif"); // gif files starting with letter a or b

            And wildcards work for sub-folder as well:

            $images = glob("*/*.gif"); // gif files in all sub-folders

            A real time-saver. One downside though is that the function is broken
            in some versions on Win32.

            Comment

            Working...