Geting last file uploaded out of a number of dir
Question
I am going to have one directory with multiple sub-directories. I want to be able to get the last, one, uploaded/modified flv out of each directory and be able create a formatted list that can be outputted.
I have used glob and foreach to create lists of folder items before but not sure how to accomplish this, and just return one file out of a number of sub-directories.
Any help will be appreciated
Thank you!
Solution
<?php
$result = array();
foreach (new DirectoryIterator('/path/to/dir') as $item) {
if (!$item->isDot() && $item->isDir()) {
$result[$item->getFilename()] = $m = null;
foreach (new DirectoryIterator($item->getPathname()) as $subItem) {
if ($subItem->isFile() && $m < $subItem->getMTime()) {
$result[$item->getFilename()] = $subItem->getFilename();
$m = $subItem->getMTime();
}
}
}
}
OTHER TIPS
You're locking for filemtime
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow