Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:05:38+00:00 2026-06-11T11:05:38+00:00

I need to exclude all files and folders from a certain directories while doing

  • 0

I need to exclude all files and folders from a certain directories while doing the recursion. I have this code so far :

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($websiteRoot.$file["filepathfromroot"]));
     foreach ($it as $currentfile)
     {
      if (!$it->isDot()&&$it->isFile()&&!in_array($it->getSubPath(), $file["exclude-directories"])) {

        //do something
         }
     }

However this subpath will only match for children and and not files and sub directories off the children. i.e For a directory structure of Foo/bar/hello.php. If you add Foo to the exclude list hello.php would still come in the result.

Does anyone have a solution for this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:05:39+00:00Added an answer on June 11, 2026 at 11:05 am

    Replace :

    in_array($it->getSubPath(), $file["exclude-directories"])
    

    By something like :

    !in_array_beginning_with($it->getSubPath(), $file["exclude-directories"])
    

    And you implement the function :

    function in_array_beginning_with($path, $array) {
      foreach ($array as $begin) {
        if (strncmp($path, $begin, strlen($begin)) == 0) {
          return true;
        }
      }
      return false;
    }
    

    But that’s not a very good way because you will recursivly get into useless directories even if they are very big and deep. In your case, I’ll suggest you to do a old-school recursive function to read your directory :

    <?php
    
    function directory_reader($dir, array $ignore = array (), array $deeps = array ())
    {
        array_push($deeps, $dir);
        $fulldir = implode("/", $deeps) . "/";
        if (is_dir($fulldir))
        {
            if (($dh = opendir($fulldir)) !== false)
            {
                while (($file = readdir($dh)) !== false)
                {
                    $fullpath = $fulldir . $file;
                    if (in_array($fullpath, $ignore)) {
                        continue ;
                    }
    
                    // do something with fullpath
                    echo $fullpath . "<br/>";
    
                    if (is_dir($fullpath) && (strcmp($file, '.') != 0) && (strcmp($file, '..') != 0))
                    {
                        directory_reader($file, $ignore, $deeps);
                    }
                }
                closedir($dh);
            }
        }
        array_pop($deeps);
    }
    

    If you try directory_reader(".", array("aDirectoryToIngore")), it will not be read at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know how to exclude files/folders from php generated zip. Here is
I have a listener for click events from which I need to exclude some
I have files a1 a2 a3 b1 b2 b3 and I need to exclude
Need some regular expressions help. So far I have my code working to allow
Is there a simple shell command/script that supports excluding certain files/folders from being archived?
I need to delete files of a certain type (.zip files, say) from a
I need an .hgdontignore file :-) to include certain files and exclude everything else
short: I need to filter all .java files and every META-INF folder from a
I need to copy a directory recursively but exclude a couple of directories within
I have a Excel CSV files with employee records in them. Something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.