You'll see in the examples in the manual that natsort() sorts the array indices as well, so that if $stack[4]='23.jpg' before the natsort, $stack[4]=23.jpg' afterwards. Funny that, the docs themselves say otherwise - 'snot a bug, 'sa feature!
This illustrates a workaround:
$stack = array();
$handle=opendir('C:\tmp\');
while (false!==($file = readdir($handle))) {
if (!is_dir($file)) {
$stack[]=$file;
}
}
closedir($handle);
print_r($stack); // Before sorting - filesystem order
natsort($stack);
print_r($stack); // After sorting - jumbled indices
$stack=array_values($stack);
print_r($stack); // After sorting - reindexed.
foreach($stack as $file)
{ echo $file.'<br>';
}