I want to sort the list of my files without taking into account Case.
Then I want to echo them out, with the case as in the folder.
Sort works but takes into account Case,
natsortcase would do what I want but it preserves the key association so they print out in the original order (at least they do on my server - on local host it seems to work correctly)
$dh = opendir($dir)) :
while (($file = readdir($dh)) !== false) :
if($file != "." && $file != ".."):
$files_names_size[$i] = $file . $findme . filesize($dir.$file);
$i=$i+1;
endif;
endwhile;
echo "<br />";
$i=$i-1;
sort($files_names_size);
// Table header.
// Fetch and print all the records.
$j=0;
while ($j < $i) :
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
// i should use explode here
$pos = strpos($files_names_size[$j], $findme);
if ($pos !== false) :
$thefilename = substr($files_names_size[$j],0,$pos);
$thefilesize = substr($files_names_size[$j], $pos+2, strlen($files_names_size[$j])-$pos-2);
else :
$thefilename = $files_names_size[$j];
$thefilesize= "inconnu";
endif;
echo '<td align="left">' .$url_name.$thefilename . '</td>';
echo '<td align="right">' . $thefilesize .' ' .'</td>';
echo '</tr>';
$j=$j+1;
endwhile;
echo '</table>';
You could use foreach instead of while, which will iterate over the array in the arrays actual order, instead forcing an iteration order by using an ascending sequence of integer keys. You could also reindex the array using array_values(), and then loop like you do now, but foreach is really the better means to your goal.
If you don’t like calling filesize in the final loop, when you create the output, there’s many ways around that let you maintain your sorting criteria.