tags:

views:

50

answers:

4

Using PHP 5.3.3 (stable) on Linux CentOS 5.5.

Here's my folder structure:

www/myFolder/
www/myFolder/testFolder/
www/myFolder/testFile.txt

Using scandir() against the "myFolder" folder I get the following results:

.
..
testFolder
testFile.txt

I'm trying to filter out the folders from the results and only return files:

$scan = scandir('myFolder');

foreach($scan as $file)
{
    if (!is_dir($file))
    {
        echo $file.'\n';
    }
}

The expected results are:

testFile.txt

However I'm actually seeing:

testFile.txt
testFolder

Can anyone tell me what's going wrong here please?

+3  A: 

Doesn't is_dir() take a file as a parameter?

$scan = scandir('myFolder');

foreach($scan as $file)
{
    if (!is_dir($file))
    {
        echo $file.'\n';
    }
}
Scott
+1 Beaten to the punch
Mark Baker
+1  A: 

If you were displaying errors, you'd see why this isn't working:

Warning: Wrong parameter count for is_dir() in testFile.php on line 16

Now try passing $file to is_dir()

$scan = scandir('myFolder'); 

foreach($scan as $file) 
{ 
    if (!is_dir($file)) 
    { 
        echo $file.'\n'; 
    } 
} 
Mark Baker
+3  A: 

You need to change directory or append it to your test. is_dir returns false when the file doesn't exist.

$scan = scandir('myFolder');

foreach($scan as $file)
{
    if (!is_dir("myFolder/$file"))
    {
        echo $file.'\n';
    }
}

That should do the right thing

Cfreak
I see where I've gone wrong now! Thanks very much!
Reado
+2  A: 

Already told you the answer here: http://bugs.php.net/bug.php?id=52471

Daniel Egeberg
You did, but I thought your answer was a bit vague without an example to back it up. Nevertheless I can see where I've gone wrong now (thanks to the examples above) and understand what you mean.
Reado
@reado: No worries. Everybody makes mistakes sometimes :)
Daniel Egeberg
Thanks Daniel, sorry for all the bug reports today. :(
Reado