onsa
12/17/2016 - 4:12 PM

List file extensions in a folder

List file extensions in a folder

<?php
	function listExtensions($dirToSearch) {
  	$dirList = array();
  	$extList = array();
  	if ($dirHandle = opendir($dirToSearch)) {
  		while ($nextEntry = readdir($dirHandle)) {
  			if ($nextEntry[0]!='.') {
  				$lastDot = 0;
  				for ($i=1;$i<strlen($nextEntry);$i++) {
  					if ($nextEntry[$i]=='.') {
  						$lastDot = $i;
  					}
  				}
  				if ($lastDot == 0) {
  					$nextEntry = '['.$nextEntry.']';
  					array_push($dirList, $nextEntry);
  				} else {
  					$extension = substr($nextEntry,$lastDot,strlen($nextEntry));
  					array_push($extList, $extension);
  				}
  			}
  		}
  		asort($dirList);
  		asort($extList);
  		foreach ($dirList as $dir) {
  			echo $dir."<br/>";
  		}
  		foreach ($extList as $ext) {
  			echo $ext."<br/>";
  		}
  	}
	}
?>