Show files from 3 directories in 3 columns and each files creation/modified date


I’m looking for some assistance with getting this working.

In the root I have an index.php file (shown below) and I also have 3 folders. Images get randomly added to these folders, what I want to do is within these 3 columns in a table is to show (sort) the latest files in each folder with their creation/modified date, or basically the date they were added.

I’ve added a screenshot to show what I have so far, the 3 columns are working fine by pulling through the latest files in each folder. However, under each image they seem to all have the same modified date, which is wrong.

Can anyone see where I’m going wrong?

    $dirname = "dir1";
    $dirnameTwo = "dir2";
    $dirnameThree = "dir3";

    $cam1 = scandir($dirname, 1);
    $cam2 = scandir($dirnameTwo, 1);
    $cam3 = scandir($dirnameThree, 1);
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' ''>

        <meta http-equiv='refresh' content='10'>
        <link rel="stylesheet" href="" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <meta http-equiv='cache-control' content='max-age=0' />
        <meta http-equiv='cache-control' content='no-cache' />
        <meta http-equiv='expires' content='0' />
        <meta http-equiv='expires' content='Tue, 01 Jan 1980 1:00:00 GMT' />
        <meta http-equiv='pragma' content='no-cache' />

    <style type="text/css">
        .pi-title {
            padding: 1rem;
    <div class="container">
        <div class="row">
            <div class="pi-title">
            <div class="table-container col-md-12">
                <table class="table" border='1' cellpadding='5' cellspacing='0' bordercolor='#ccc'>
                    <thead class="thead-dark">
                        <th scope="col">ID</th>
                        <th scope="col">1</th>
                        <th scope="col">2</th>
                        <th scope="col">3</th>
                        error_reporting(E_ALL & ~E_NOTICE);
                        array_multisort(array_map('filemtime', ($files = glob("*.*", GLOB_BRACE))), SORT_DESC, $files);
                        $dirs = array($dirname, $dirnameTwo, $dirnameThree);
                        foreach ($files as $filename) {
                                        for ($i = 1; $i <= 12; $i++) {
                                        if (file_exists($filename)) {
                                            echo "</tr>";
                                            echo "<td><font face='Arial' size='6'>$i</font></td>";
                                        echo "<td><img src='$dirs[0]/$cam1[$i]' height='180' width='220'><br>" . date("F d Y H:i", filemtime($filename));
                                        echo "</td>";
                                        echo "<td><img src='$dirs[1]/$cam2[$i]' height='180' width='220'><br>" . date("F d Y H:i", filemtime($filename));
                                        echo "</td>";
                                        echo "<td><img src='$dirs[2]/$cam3[$i]' height='180' width='220'><br>" . date("F d Y H:i", filemtime($filename));
                                        echo "</td>";
                               if ($i === 12) break;
                            if ($i === 12) break;


Dont have an image to look at.

So the file modified time is the same for every file? Or is the same for every file on that row? On that row makes sense.


This code is pretty confused about what it is trying to do.

The $camx arrays are sorted in descending filename order and the index starts at zero, so unless the filenames start with date/time values in a sort-able format, you are not getting the newest files first and the first array entry, with a zero index, is being skipped (the for(){} loop starts at one.) The dot and dot-dot entries are also present in these arrays, which you don’t want to try to use as part of img src=’…’ attributes.

In the displayed code (could be due to mangling by this forum software), the glob() statement is getting the contents of the current folder, where this script is at, not the content(s) of the three folders. If the line where the glob() statement is at actually has the three folders listed inside of { … }, then you could be getting all the files spanning the three folders sorted by their filetime, assuming that’s what you want.

So, to start with, what result do you want. Do you want column 1 to hold a maximum of the 12 latest files from dir1, column 2 to hold a maximum of the 12 latest files from dir2, and column 3 to hold a maximum of the 12 latest files from dir3, and what do you want to happen where there are less than 12 files for a column OR do you want to get the 36 latest files spanning the three folders and display those from newest to oldest, left to right in the html table?

To produce the first result, you need to execute the line containing the glob() statement for each of the three folders. You can do this dynamically by having an array of the folders (which you already have in $dirs), use a foreach(){} loop to loop over the $dirs array, and store the sorted glob() result in an array of arrays. This is all the code you need to get the file information.

To produce the output from this data, you need a main loop, from 0 to 11, and a nested loop, from 0 to 2 (just loop over the $dirs array again) and reference the correct index in the array of arrays of sorted glob() result, adding a conditional test to handle the case where there are less than 12 entries (the element in the array being referenced won’t be set.) You would get the filemtime() of each file as you are outputting it in the html table.