PHP - Get a list of files in a directory for date range sorted by last modified time

I need to read one directory and create an array with the list of XML files created/modified over the last two days sorted by descending modified time.  I will iterate through this array to get data for multiple items that will be displayed in a web page.
LVL 4
rbartzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
PHP scandir() can give you the list of file (IIRC, so can glob()).  Then you can use foreach to iterate over the list and acquire the filemtime() information.  I would suggest making an array of objects with one object representing each desired file.  You can sort the array of objects to order them in any way that makes sense.
0
rbartzAuthor Commented:
Thank you Ray for your suggestion.  There may be hundreds of files in this directory at any given time.  I just want the ones that have modified times within the past two days.  scandir() will give me a list of files and blob() will return the XML ones but then going back out for filetimes on each file for filtering and then sorting the array seems to me to be terribly inefficient.    Since the site in on a linux OS then we should be able to get the file list sorted by date and mtime.
0
Ray PaseurCommented:
...sorting the array seems to me to be terribly inefficient.
Are we talikng about millions and millions of data points?  If not, efficiency is not relevant.  Please show us the code you have tried to use for this application.  If it can be improved we can surely offer suggestions!
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

rbartzAuthor Commented:
The "inefficient" point is based on the fact that there are somewhere between 200 and 600 page loads an hour a couple of hours every day.   The page needs to display 12 or more complete, formatted items with images besides anywhere between 100 to 300 archived item titles and dates with links.   I probably can use caching for the archives but the file reads for the rest of the items have to be done because the files change frequently.

I have not tried to code it yet.  I suppose I can use find with ls to get the latest 12 files.  I will work on code tomorrow and post what I have if it does not work the way I need it to.
0
Ray PaseurCommented:
I would start with scandir() and foreach() and filemtime() -- If you're only talking a few hundred files and a few hundreds hits per hour this will not be a performance problem at all.  600 hits per hour == 10 hits per minute == <1 hit per second.  That's not a load to worry about!  You can use the standard PHP microtime() to see how long it takes to run the script.  My guess is that it will be almost unnoticeable.

No matter how you get the information, these same steps will be followed.  The only question is whether you will control the process or some other piece of programming will control the process.
0
Ray PaseurCommented:
This should help you get started:
<?php // demo/temp_rbartz.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28660035.html
 * http://php.net/manual/en/function.scandir.php
 * http://php.net/manual/en/function.filemtime.php
 * http://php.net/manual/en/array.sorting.php
 */
error_reporting(E_ALL);

// CHOOSE DATE LIMIT, DIRECTORY AND FILE TYPE
$dat = date('c', strtotime('TODAY - 2 DAYS'));
$dir = !empty($_GET['q']) ? $_GET['q'] : getcwd();
$end = !empty($_GET['s']) ? strtoupper($_GET['s']) : 'XML';

// GET DIRECTORY FILE LIST
$arr = scandir($dir, SCANDIR_SORT_ASCENDING);

// FILTER FOR FILE SUFFIX AND DATE
$new = [];
foreach ($arr as $fnm)
{
    $suf = explode('.', $fnm);
    $suf = end($suf);
    $suf = strtoupper($suf);
    if ($suf != $end) continue;
    $ftm = date('c', filemtime($fnm));
    if ($ftm > $dat) $new[$fnm] = $ftm;
}

// IF NOTHING PASSES THE FILTER
if (empty($new)) die("NO FILES OF TYPE '$end' SINCE $dat");

// SORT BY DATE DESCENDING
arsort($new);

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($new);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rbartzAuthor Commented:
Thank you Ray, I will use that as a basis for the page and begin testing.  It does list the directory and sorts the files.
0
Ray PaseurCommented:
Gr8!  Thanks for the points and thanks for using E-E, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.