Jump to content

show last 5 modified folders


Onloac

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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++;
  }
}
?>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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))

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.