Find most recently created directory (not file!)

Consider the following directories:

[font=courier]archive/20140420-1500/
archive/20140420-1800/
archive/20140421-1000/[/font]

Yes, this are essentially date-time stamps used as directory names. The contents of the directories are irrelevant to my problem, but they contain HTML files.

Parallel to [font=courier]archive/[/font] is the directory named [font=courier]current/[/font].

What I would like to do is this: whenever someone points their browser to [font=courier]current/index.html[/font] (or just [font=courier]current/[/font] as the index file will be pulled up by default) they would redirect to the most recent directly in [font=courier]archive/[/font].

So in the example above, if I point my browser to [font=courier]current/[/font] I would be redirected to [font=courier]archive/20140421-1000/[/font].

But if I later add a new directory named [font=courier]archive/20140421-1100/[/font] then [font=courier]current/[/font] would redirect me there, without me having to update anything.

I suppose this could be done one of two ways:

  1. Redirect to most recently created directory
    -or-
  2. Redirect to the [font=courier]archive/[/font] subdirectory with the highest name value (since my naming convention puts things in sequence with the most recent date at the bottom of the list.)

The redirect I can handle with a simple HTML [font=courier]meta[/font] redirect. Just having trouble using PHP to scan the archive directory and dynamically generate HTML to point to the most recent archive folder.

If there is an even easier way to go about this, I’m all ears. I have a suspicion this could be done with .htaccess as well, but I’m even less competent in that than I am in PHP.

Really appreciate any help! Many thanks.

Why would you want to use HTML META’s…
First you are using PHP to grab the directory names, just redirect from it.

So, here are some ideas to get you started…

Create an array.
$dirs = array();

load all the files into it using a handle to the folder which contain the files, like “archive”.
$dir_handle = opendir(“my-path-to/archive/”); (Loads all filenames including “.” “…”)

Parse thru then to get the real directories…
while (($filename = readdir($dir_handle)) !== false) {
// Each $filename in this loop is a file in the directory…
// Use as needed, but, can check if it is a directory this way: (1st check for “.” and “…”)
if ($filename != “.” && $filename != “…”) {
if (is_dir($filename)) {
// Here $filename is a directory, load it into your array of Dirs or just compare it to the saved one
$dirs .= $filename;
}
}
}

Something like that would grab only the directories in your archive folder. Then, you can compare them. Or, you can just save one and compare the others. Many ways to do this. The easiest is to just use the array itself. So, you have all the Directories except “.” and “…” at this point in the $dir variable. Just do an array check to grab the highest one, like this: $target_dir = max($dirs); That grabs the biggest one which with your setup should be the most recent. (Sometimes dashes cause issues, so you might have to change this to another check…)

Then, you have the target directory, just redirect to it using standard PHP commands…
Like: header("Location: " . $target_dir);
Note, you can add direct locations such as header(“Location: http://www.my-second-site.com/” . $target_dir); if it is on a second server. But, the first options works if page is on same server…

Hope that helps get you going. All of this is not tested for you, that is your job. Good luck.

Sponsor our Newsletter | Privacy Policy | Terms of Service