Jump to content

Recommended Posts

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?

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

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

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.

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

 

 

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

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

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

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.