Can anyone please help me exclude the .php files from this code?

<?php

$dir = 'x'; 
$filecount = 0; 
$d = dir($dir); 
while ($f = $d->read()) { 
 if(($f!= ".") && ($f!= "..")) { 
 if(!is_dir($f)) $filecount++; 
 } 
} 
echo 'there are ',$filecount,' files in this folder';


?>

I have tried to use ereg function but had no luck :(

ereg('\.php', $f)

Recommended Answers

All 3 Replies

How about strstr()?

while ($f = $d->read()) { 
    if ( strstr( $f, ".php" ) continue; //skip this one
    [...rest of the code....]

But be aware that this is case-sensitive. So .PHP file will not be detected!

I tried it like this:

<?php

$dir = 'x'; 
$filecount = 0; 
$d = dir($dir); 
while ($f = $d->read()) { 
 if(strstr( $f, ".php" )) continue;
 if(($f!= ".") && ($f!= "..")) { 
 if(!is_dir($f)) $filecount++;
 } 
} 
echo 'there are ',$filecount,' files in this folder';


?>

But it still shows me all the files including the .php in the sub directories.

OK, so probably you have .PHP files instead of .php files.
Try replacing:

if(strstr( $f, ".php" )) continue;

with:

if(stristr( $f, ".php" )) continue;

stristr is the case-insensitive function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.