USING PHP TO (Com web hosting) MANAGE FILES $fileTypes = array(’jpg’,'jpeg’,'gif’,'png’);
USING PHP TO MANAGE FILES $fileTypes = array(’jpg’,'jpeg’,'gif’,'png’); // traverse folder, and add file to $found array if type matches $found = array(); foreach ($contents as $item) { $fileInfo = pathinfo($item); if (array_key_exists(’extension’, $fileInfo) && . in_array($fileInfo[’extension’],$fileTypes)) { $found[] = $item; } } // Check the $found array is not empty if ($found) { // sort in natural, case-insensitive order, and populate menu natcasesort($found); foreach ($found as $filename) { echo “n”; } } } } How the buildFileList() function works I suspect many readers will be happy just to use the function, but if you re curious as to how it works, here s a brief description to flesh out the inline comments. After the folder has been opened, each item is passed to a PHP function called pathinfo(), which returns an associative array with the following elements: dirname: The name of the directory (folder) basename: The filename, including extension (or just the name if it s a directory) extension: The filename extension (not returned for a directory) Because the extension element is not returned for a directory, you need to use array_key_exists() before attempting to check its value. The second half of the condi tional statement in line 12 uses in_array() to see if the value of extension matches one of the file types that you re looking for. It there s a match, the filename is added to the $found array. It s then just a case of building the