Read files and folders recursively

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    Read files and folders recursively

    Hi,

    Is there a way to recursively select folders and upload the files in the current, so to speak, folder?

    I know that there exists a possibility to upload a file in the same folder that the php script is executed, what I'm really looking for is a way to loop through all the folders.

    Thanks in advance!
  • satas
    New Member
    • Nov 2007
    • 82

    #2
    Yes, it's possible with glob() function :
    http://php.net/manual/en/function.glob.p hp

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Originally posted by satas
      Yes, it's possible with glob() function :
      http://php.net/manual/en/function.glob.p hp
      glob() is not recursive. It is easier and a lot faster to use opendir() and readdir().

      Ronald

      Comment

      • satas
        New Member
        • Nov 2007
        • 82

        #4
        Originally posted by ronverdonk
        glob() is not recursive. It is easier and a lot faster to use opendir() and readdir().

        Ronald
        Yes it is. I've meant that it is much easier to use glob() function to get folder's tree instead of odendir+readdir . Of course, you should write a function which will be able to scan folders. I'll post here my own written function for it, if ill find it :)

        Comment

        • satas
          New Member
          • Nov 2007
          • 82

          #5
          Here it is:

          [PHP]
          function ShowTree($paren t_dir) {
          //node counter
          static $i = 0;
          $i++;

          $dirs = glob($parent_di r . '/*', GLOB_ONLYDIR);
          $cnt = count($dirs);

          if ($cnt > 0) {
          print("<ul>");

          foreach ($dirs as $dir) {
          // strip ../
          print substr($dir, 3, strlen($dir));
          ShowTree($dir);
          }

          print("</ul>");
          }
          }
          [/PHP]

          Note: i've just strip a lot of unnecessary code so function may not work (can't test it at the moment). But the main idea is clear, i hope.

          Comment

          Working...