Read files and list them with natural sorting in PHP

 <?php

 $file_arr = array();

 $dd = dir('uploads');   // files in this relative path

  while ( $file = $d->read())  != false ) {

       if(strlen($file)>2) {   // ./ and ../ is exceptional

          $file_arr[]=$file;

       }

   }

 $d->close();   // close file ptr

sort($file_arr,SORT_NATURAL);   // or  natsort($file_arr);

$i=1;

foreach ($file_arr as  $sf){

  echo $i.". ".$sf."<br>";

}   // print all sorted files

?>


References

https://www.php.net/manual/en/function.sort.php

 https://www.php.net/manual/en/function.natsort.php

 https://www.php.net/manual/en/function.natcasesort.php


這個網誌中的熱門文章

取消Excel與word網際網路與網路路徑超連結自動格式

Installing VMware workstation pro on Ubuntu 22.X (Jammy Jellyfish)