php RecursiveDirectoryIterator: how to exclude directory paths with a dot and double dots?

10,343

Try with :

new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory, RecursiveDirectoryIterator::SKIP_DOTS);

See http://us1.php.net/manual/en/class.filesystemiterator.php

Share:
10,343
Run
Author by

Run

A cross-disciplinary full-stack web developer/designer.

Updated on June 28, 2022

Comments

  • Run
    Run almost 2 years

    RecursiveDirectoryIterator seems to give me two different results from my localhost and live server,

    define ('WEBSITE_DOCROOT', str_replace('\\', '/', dirname(__FILE__)).'/');
    
    print_r(WEBSITE_DOCROOT);
    
    // List all the class directories in the array.
    $main_directories = array(
        'core/model/',
        'core/helper/',
        'core/ext/'
    );
    
    // Set other vars and arrays.
    $sub_directories = array();
    
    // List any sub dirs in the main dirs above and store them in an array.
    foreach($main_directories as $path_directory)
    {
        $iterator = new RecursiveIteratorIterator
        (
            new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory), // Must use absolute path to get the files when ajax is used.
            RecursiveIteratorIterator::SELF_FIRST
        );
    
        foreach ($iterator as $fileObject) 
        {
            if ($fileObject->isDir()) 
            {
                //if($fileObject->isDir() === '.' || $fileObject->isDir() === '..') {continue;} 
    
                // Must trim off the WEBSITE_DOCROOT. 
                $sub_directories[] = preg_replace('~.*?(?=core|local)~i', '', str_replace('\\', '/', $fileObject->getPathname())) .'/';
            }
        }
    }
    
    // Mearge the main dirs with any sub dirs in them.
    $merged_directories = array_merge($main_directories,$sub_directories);
    print_r($merged_directories);
    

    localhost,

    (
        [0] => core/model/
        [1] => core/helper/
        [2] => core/ext/
    )
    

    live server,

    (
        [0] => core/model/
        [1] => core/helper/
        [2] => core/ext/
        [3] => core/model/./
        [4] => core/model/../
        [5] => core/helper/./
        [6] => core/helper/../
        [7] => core/ext/./
        [8] => core/ext/../
    )
    

    So, how can I exclude the directory paths with a dot and double dots?

    EDIT:

    Live server - PHP version 5.3.27 Localhost - PHP version 5.5

  • Ion
    Ion over 10 years
    Great! I was looking for the same thing, I was excluding them by hand (if dot/dotdot continue)
  • Brice
    Brice over 10 years
    Then I guess we'll need more information about your server, the version of PHP you are running, etc. (update your question consequently)
  • Brice
    Brice over 10 years
    php.net states PHP 5 >= 5.3.0. I tried with PHP 5.3.26 and it works. Could you may be try again? :/
  • NoWomenNoCry
    NoWomenNoCry about 6 years
    it works - avoids dots, problems may be in other places of code (I am trying to zip a dir recursively - but it is other topic)