Scandir() sorting

I’m trying to use scandir() to sort in descending order but I’m not clear from the manual how to use the sort option. I’m running PHP v5.3.1 so I can’t use the numeric values. I have tried

scandir($years, SCANDIR_SORT_DESCENDING);

but get an error message:
Warning: scandir() expects parameter 2 to be long, string given in C:\xampp\htdocs\www\ifold\minutes.php on line 26
Thanks G

I’ve seen this before. Check that the SCANDIR_SORT_DESCENDING constant has been defined:


<?php
var_dump(get_defined_constants());

Look for that constant (or any beginning with SCANDIR_)

Thanks Antnee. It looks like the SCANDIR constants are not set.

Thought so. They were added in PHP 5.4, so you’ll need to use the integer values. 0 is ascending, 1 is descending IIRC (why I didn’t just ask whether you were using PHP 5.4 or not I have no idea!)

Oh. I thought the manual said the integer values were added in 5.4 and I thought that I couldn’t use them! Guess I read that the manual wrong…

When you have these functions with constants associated with sorting orders etc, they’re always just passing a value that you could enter directly. However, the values of these constants MAY change, which is why it’s better to use the constant. Constants will always follow (or appear at the same time) as the integer values

Many thanks for your useful explanation. G :slight_smile:

You could use this code:

if(!defined("SCANDIR_SORT_ASCENDING")) {
    define("SCANDIR_SORT_ASCENDING", 0);
}
if(!defined("SCANDIR_SORT_DESCENDING")) {
    define("SCANDIR_SORT_DESCENDING", 1);
} else {
    // send me an email telling me this code is no longer needed
}

Thanks Denny - that’s useful. G :slight_smile: