0

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)

Edited by seularts: forgot soething

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by n_e
0

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!

Edited by n_e: n/a

0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.