Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ignoring Directories

Status
Not open for further replies.

axman505

Technical User
Jun 20, 2001
489
US
I have the following chunk of code. The ignore array contains directory names which should be ignored when outputed. However, there is a flaw in the logic that i cannot figure out, it doesnt work right at all, any suggestions? Thanks.


$ignore =
array(".","..","extlib","images","lib","plugins","schemas","search_templates","tmpl");

if ($handle = opendir('/path/to/folder/')) {

while (false !== ($file = readdir($handle))) {
if (is_dir($file)){
for ($i=0; $i<sizeof($ignore); $i++){
if (preg_match(&quot;/$file/i&quot;,$ignore[$i])) {
print &quot;$file = $ignore[$i]<br>&quot;;
} else {
print &quot;$file<br>&quot;;
}
}
}
}
closedir($handle);
}
 
If you look at my loop, i do make use of the is_dir() function
 
A suggestions:

Your ignore list is an array. You need not compare each entry to the filename in a loop every time. Have a look at the in_array() function
Here's some more compact code:
Code:
$ignore =
array(&quot;.&quot;,&quot;..&quot;,&quot;extlib&quot;,&quot;images&quot;,&quot;lib&quot;,&quot;plugins&quot;,&quot;schemas&quot;,&quot;search_templates&quot;,&quot;tmpl&quot;);

if ($handle = opendir('/path/to/folder/')) {

    while (false !== ($file = readdir($handle)))  {
       if (is_dir($file) && in_array($file,$ignore)){
           print &quot;$file ignored<br>&quot;;
       } else {
          print &quot;$file<br>&quot;;
       }
    }
    closedir($handle);
}
 
was not aware of the in_array function, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top