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...

$counter=0;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
       		$filetype=(pathinfo($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.

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);
var_dump($files);
...
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.