You can use the filesize($file)
function to check whether we are dealing with a category or file. Your example in this case will look like this:
$dirct= $_SERVER['DOCUMENT_ROOT'].'/main/'; $hdl=opendir($dirct); while ($file = readdir($hdl)) if ( ($file!=".")&&($file!="..")) { if(filesize($file)) $files[]=$file; else $dirs[] = $file; } closedir($hdl); usort($files,"strcmp"); //Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠ°ΠΉΠ»Ρ foreach ($files as $value) { echo ("<a href = /main/$value>".$value."</a><br/>") ; } //Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΠΈ usort($dirs,"strcmp"); foreach ($dirs as $value) { echo ("<a href = /main/$value>".$value."</a><br/>") ; }
You can also add the use of the usort () function; Like this:
function mySort($file_a, $file_b){ //ΡΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² //Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ: //-1 Π΅ΡΠ»ΠΈ $file_a Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±ΡΡΡ Π½ΠΈΠΆΠ΅ ΡΠ΅ΠΌ $file_b // 1 Π² ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠΉ ΡΠΈΡΡΠ°ΡΠΈΠΈ // 0 Π΅ΡΠ»ΠΈ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½Ρ $filesize_a = filesize($file_a); $filesize_b = filesize($file_b); if (!$filesize_a && !$filesize_b || $filesize_a && $filesize_b) return strcmp($file_a, $file_b); else if ($filesize_a) return -1; else return 1; } $dirct= $_SERVER['DOCUMENT_ROOT'].'/main/'; $hdl=opendir($dirct); while ($file = readdir($hdl)) if ( ($file!=".")&&($file!="..")) { $a[]=$file; } closedir($hdl); usort($a,"mySort"); foreach ($a as $value) { echo ("<a href = /main/".$value.">".$value."</a><br/>") ; }
This code will display links to files first, then to folders. Playing with the values ββreturned by the mySort () function, you can change the sorting.