Example use of JFolder to load files from a folder filtering them by extension. We also want to allow extensions in uppercase.
Our test will be done in a folder with this sample content:
From that folder we only want to get the .jpg, .png, and .gif files. We will process it with this code:
// Sample folder $imgsPath = JPATH_SITE . "/images/sampledata/fruitshop"; if (is_dir($imgsPath)) { // Allowed filetypes $allowedExtensions = array('jpg','png','gif'); // Also allow filetypes in uppercase $allowedExtensions = array_merge($allowedExtensions, array_map('strtoupper', $allowedExtensions)); // Build the filter. Will return something like: "jpg|png|JPG|PNG|gif|GIF" $filter = implode('|',$allowedExtensions); $filter = "^.*\.(" . implode('|',$allowedExtensions) .")$"; $files = JFolder::files($imgsPath, $filter); if ($files) { echo ''; print_r($files); echo ''; // Do something } else echo 'No files found'; }
Will return something like:
Array ( [0] => filezilla.png [1] => fireworks.png [2] => fireworks2.PNG [3] => fondomenu.JPG [4] => heidisql.png )