views:

47

answers:

5

For some reason, I keep getting a '1' for the file names with this code:

if (is_dir($log_directory))
{
    if ($handle = opendir($log_directory))
    {
        while($file = readdir($handle) !== FALSE)
        {
            $results_array[] = $file;
        }
        closedir($handle);
    }
}

When I echo each element in $results_array, I get a bunch of '1's, not the name of the file. How do I get the name of the files?

A: 

There's a few options using glob.

Inkspeak
+1  A: 

Just use glob('*'). Here's Documentation

Fletcher Moore
+2  A: 

Don't bother with open/readdir and use glob() instead:

foreach(glob($log_directory.'/*.*') as $file) {
    ...
}
Tatu Ulmanen
You might want to wrap an `array_filter(..., 'is_file')` around that glob since the question asks for files.
salathe
A: 

It's due to operator precidence. Try changing it to:

while(($file = readdir($handle)) !== FALSE)
{
    $results_array[] = $file;
}
closedir($handle);
ircmaxell
A: 

You need to surround $file = readdir($handle) with parentheses.

Here you go:

$log_directory = 'your_dir_name_here';

$results_array = array();

if (is_dir($log_directory))
{
        if ($handle = opendir($log_directory))
        {
                //Notice the parentheses I added:
                while(($file = readdir($handle)) !== FALSE)
                {
                        $results_array[] = $file;
                }
                closedir($handle);
        }
}

//Output findings
foreach($results_array as $value)
{
    echo $value . '<br />';
}
letseatfood