Search code examples
phpsortingscandir

Sorting functionality scandir function bug


I'm experiencing some unexpected behaviour with the scandir function. The documentation says it is sorting the files and folders default by ascending order. Now I have a folder with six files in it:

__Abstract.coffee
__Abstract.js
Copy.coffee
Copy.js
Delete.coffee
Delete.js

Now it appears to be that PHP's scandir output is most of the times:

array (
  0 => '.',
  1 => '..',
  2 => '__Abstract.coffee',
  3 => '__Abstract.js',
  4 => 'Copy.coffee',
  5 => 'Copy.js',
  6 => 'Delete.coffee',
  7 => 'Delete.js',
)

But sometimes (1 times in 50 or so) it puts the __Abstract.coffee and __Abstract.js at the end so the returned value is:

array (
  0 => '.',
  1 => '..',
  2 => 'Copy.coffee',
  3 => 'Copy.js',
  4 => 'Delete.coffee',
  5 => 'Delete.js',
  6 => '__Abstract.coffee',
  7 => '__Abstract.js',
)

Because of this the JS files are sometimes not loaded in the right order (it is a js packer/minifier script).

I know how to work around this with the sort functions, but that is not very efficient if the scandir function should order the files by itself. Is this a bug in the scandir function and should I report this in the php bugtracker or is it more like a filesystem/OS thing (I'm using a default LVM installation under CENTOS 6.3).

Can you guys please help me with some advice :-) thank you!

I have sent in a bugreport at PHP: https://bugs.php.net/bug.php?id=65709


Solution

  • It looks like this has something to do with the intl extension. When I disable it, the problem does not occure. It doesn't make a diffrence if the extension is installed with pecl or compiled using --enable-intl. I will catch up with the developers of the intl extension for the solution to this problem.