Hi all,

Can anyone shed any light on this really simple problem please?

I need to read a list of files in a directory on a server. I am using PHP to do something like this...

if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {

I then decide using an if(($filetype[extension]=="txt")||($filetype... to choose what to do with the file.

The problem is this. the readdir($handle) function does not return filenames if the filename has a blank space within its name. So it will read "myfile.txt" okay, but will skip "my file.txt" the problem seems to be the space in the filename because if I manually remove the spaces (from my test files) the function works perfectly well.

Oh and by the way, unfortunately I cannot guarantee file-name consistency in here, so I have to assume there will be names with spaces!

Any suggestions would be really appreciated. thanks in advance, JohnB.

9 Years
Discussion Span
Last Post by petr.pavel

Hi John,
I'm reluctant to believe you on the spaces issue with readdir(). I checked the manual and nobody in the discussion has ever mentioned it. Maybe you think that it skips the files but in fact, something else down the road in your script does.

Anyways, try using glob() instead. It will make your script look prettier :-)
Something along these lines:

$pattern = "$dir/*.txt";
$files = glob($pattern);
This question has already been answered. 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.