Onloac Posted August 18, 2010 Share Posted August 18, 2010 hi, I've tried searching google without luck so I thought I would try here. I'm trying to display the names of the last 5 modified folders within a directory i provide. Does anyone know a tutorial or site that could teach me how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/ Share on other sites More sharing options...
JonnoTheDev Posted August 18, 2010 Share Posted August 18, 2010 windows or linux? Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1100705 Share on other sites More sharing options...
Onloac Posted August 18, 2010 Author Share Posted August 18, 2010 Linux. I just need something simple that will show the names of the last 5 modified folders within a given directory. I've been looking around for something for ages. =( Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1100712 Share on other sites More sharing options...
Psycho Posted August 18, 2010 Share Posted August 18, 2010 What do you mean by the "modified" date for folders? I know you said this was for Linux, but in Windows at least, there is no modified date for folders - only creation dates. Are you wanting the date the folder was created OR the last time something inside the folder was changed? Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1100726 Share on other sites More sharing options...
Wolphie Posted August 18, 2010 Share Posted August 18, 2010 Give this a try: <?php $path = 'path/to/dir'; if (is_dir($path)) { $contents = scandir($path); foreach ($contents as $file) { $full_path = $path . DIRECTORY_SEPARATOR . $file; if ($file != '.' && $file != '..') { if (is_dir($full_path)) { $dirs[filemtime($full_path)] = $file; } } } // Sort in reverse order to put newest modification at the top krsort($dirs); $iteration = 0; foreach ($dirs as $mtime => $name) { if ($iteration != 5) { echo $name . '<br />'; } $iteration++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1100729 Share on other sites More sharing options...
Psycho Posted August 18, 2010 Share Posted August 18, 2010 If you want the last 5 folders by their creation/modified date, this should work. (only tested in windows): //Specify the root directory $root_directory = "./rootfolder"; //Get list of folders in root $foldersAry = glob("{$root_directory}/*", GLOB_ONLYDIR); //Create array of folder with date $foldersByDate = array(); foreach($foldersAry as $folder) { $foldersByDate[basename($folder)] = date('Y-m-d', filemtime($folder)); } //Sort array by date arsort($foldersByDate); //Remove all but the first five folders array_splice($foldersByDate, 5); print_r($foldersByDate); Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1100732 Share on other sites More sharing options...
Onloac Posted August 18, 2010 Author Share Posted August 18, 2010 Sorry, I should have been more specific. I was looking to show the 5 last folders sorted by "the last time something inside the folder was changed". For example, I have images within a directory. I want to list the last 5 folders to have images added to it. Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1101001 Share on other sites More sharing options...
Psycho Posted August 19, 2010 Share Posted August 19, 2010 Here you go. There are two functions: getFoldersByModified(): This function takes a root folder as the input and will check each folder using lastModifiedDate() (explained below) and will return an array of the folders in order of the date that their content was last modified. lastModifiedDate(): This will return the last modified date of anything inside the folder (files and other subfolders). You can then take the first five elements from the returned array using something like array_splice($array, 5); function lastModifiedDate($folder, $lastDate=false) { $filesAry = glob("{$folder}/*"); foreach($filesAry as $file) { if(!$lastDate || filemtime($file)>$lastDate) { $lastDate = filemtime($file); } if(is_dir($file)) { $lastDate = lastModifiedDate($file, $lastDate); } } return $lastDate; } function getFoldersByModified($root_directory) { //Get list of folders in root $foldersAry = glob("{$root_directory}/*", GLOB_ONLYDIR); //Iterrate through each folder getting last modified date $foldersByDate = array(); foreach($foldersAry as $folder) { $foldersByDate[lastModifiedDate($folder)] = basename($folder); } krsort($foldersByDate); return $foldersByDate; } //Specify the root directory $root_directory = "./ts3-v3-small"; //Get all folders in order of the "modified" date $foldersByDate = getFoldersByModified($root_directory); //Remove all but the first five array_splice($foldersByDate, 5); print_r($foldersByDate); Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1101086 Share on other sites More sharing options...
JonnoTheDev Posted August 19, 2010 Share Posted August 19, 2010 hi, I've tried searching google without luck so I thought I would try here. Your first port of call should be the php manual. There are also loads of examples here. http://uk.php.net/filemtime Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1101126 Share on other sites More sharing options...
Onloac Posted August 19, 2010 Author Share Posted August 19, 2010 Thanks! You guys have been very helpful as usual. I'll study the code and read up on filemtime. Thanks again! mjdamato@ did u have that code made already or did you just right it up? You hit the nail on the head... that code couldn't have been better. =p Thanks alot! I'm gonna alot from it. Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1101149 Share on other sites More sharing options...
Psycho Posted August 19, 2010 Share Posted August 19, 2010 mjdamato@ did u have that code made already or did you just right it up? I wrote it yesterday. If I already had it I would have posted it in my first response. It seemed like an interesting problem to solve and I thought that I could get some "template" code that I could use in the future. The only thing is that it is not going to be efficient since you have to check every object inside the folder. I also made the code recursive so when it checks to find when the content of a folder was last modified it not only check the first level of files and folder it also check all the sub folders and the subfolders in those, and so on. If you don't need to check the subfolders then remove the entire IF condition block that starts if(is_dir($file)) Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1101372 Share on other sites More sharing options...
Psycho Posted August 19, 2010 Share Posted August 19, 2010 After a little more tinkering I've greatly reduced the primary function that determines the last time any contents of a folder have been modified. function lastModifiedDate($folder, $recursive=true) { $filesAry = glob("{$folder}/*"); foreach($filesAry as $file) { $lastMod = max(filemtime($file), (is_dir($file)&&$recursive)?lastModifiedDate($file):0); } return $lastMod; } Quote Link to comment https://forums.phpfreaks.com/topic/211057-show-last-5-modified-folders/#findComment-1101399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.