scandir() Validates Its Sorting Order Argument¶
scandir() accepts a second argument that selects the sort order of the returned entries: SCANDIR_SORT_ASCENDING, SCANDIR_SORT_DESCENDING, or SCANDIR_SORT_NONE. Until PHP 8.6, any other value was silently accepted and treated as descending order. In PHP 8.6, an invalid sorting order throws a ValueError.
PHP code¶
<?php
$dir = sys_get_temp_dir() . '/scandir_86_test';
@mkdir($dir);
touch($dir . '/a.txt');
touch($dir . '/b.txt');
try {
var_dump(scandir($dir, 99));
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
?>
Before¶
array(4) {
[0]=>
string(5) b.txt
[1]=>
string(5) a.txt
[2]=>
string(2) ..
[3]=>
string(1) .
}
After¶
scandir(): Argument #2 ($sorting_order) must be one of the SCANDIR_SORT_ASCENDING, SCANDIR_SORT_DESCENDING, or SCANDIR_SORT_NONE constants
PHP version change¶
This behavior changed in 8.6