1

I have the following PHP code. It gets the files from the folder ../sessions. I believe it's currently sorted (I think) by alphabet.

How would I sort the table by file creation date?

<?php

$path = "../sessions"; 

$latest_ctime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path}/{$entry}";
  // could do also other checks than just checking whether the entry is a file
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
    $latest_ctime = filectime($filepath);
    $latest_filename = $entry;
  }
}

$files_in_directory = scandir('../sessions');
$items_count = count($files_in_directory);
if ($items_count <= 2)
{
    $empty = true;
}
else {
    $empty = false;
}
echo $empty;

$files_in_directory2 = scandir('../sessions');
$items_count2 = count($files_in_directory2);
if ($items_count2 <= 2)
{
    $empty2 = true;
}
else {
    $empty2 = false;
}
echo $empty2;
    $fileNameNoExtension = array_pop(array_reverse(explode(".txt", $latest_filename)));
    if($empty == false) { echo "<br />Nieuwste sessie: "; }
    elseif ($empty == true) { echo "Geen sessies aangemeld"; }
    if ($fileNameNoExtension == '.dont-delete-htaccess') { echo "Geen sessies aangemeld<br /><br />"; }
    if($empty == false && $fileNameNoExtension !== '.dont-delete-htaccess') { echo "<a href='vimeo.php?id=$fileNameNoExtension'><strong>$fileNameNoExtension</strong></a><br /><br />"; };

  function getFileList($dir)
  {
    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry/",
          "lastmod" => filemtime("$dir$entry")
        );
      } elseif(is_readable("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry",
          "lastmod" => filemtime("$dir$entry")
        );
      }
    }
    $d->close();

    return $retval;
  }
    $dirlist = getFileList("../sessions");

  // Output file list in HTML TABLE format
  echo "<table class='table' border=\"1\">\n";
  echo "<thead>\n";
  echo "<tr><th>E-mailadres <small>(pad)</small></th><th>Status</th><th>Subbezoekers<br /><small>Bezoekers onder dezelfde sessie</small></th><th>Tijd & Datum</th></tr>\n";
  echo "</thead>\n";
  echo "<tbody>\n";
  foreach($dirlist as $file) {
  $foreach=$file['name'];
  $url=substr($foreach, strrpos($foreach, '/') + 1);
  $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $url);
    echo "<tr>\n";

    # Owner naam krijgen

    $owner = '../sessions/'.$withoutExt.'.txt';
    $fp2 = fopen($owner, "r");

    $contentname = fread($fp2, filesize($owner));
    $namer = explode("\n", $contentname);
    fclose($fp2);
    $namerln = $namer['0'];

    # Unieke rand krijgen

    $rand = '../sessions/'.$withoutExt.'.txt';
    $fp3 = fopen($rand, "r");

    $contentid = fread($fp3, filesize($rand));
    $lineid = explode("\n", $contentid);
    fclose($fp3);
    $idln = $lineid['7'];

    echo "<td><a href='vimeo.php?id=$withoutExt'><strong>$withoutExt</strong> (ID: $idln) (naam: <strong style='font-size: 20px;'>$namerln</strong>)<br /><small>({$file['name']})</small></a></td>\n";
    echo "<td>";
    if (substr(sprintf('%o', fileperms('../sessions/'.$withoutExt.'.txt')), -4) == '0664') {
    echo '<p style="color: red;">Niet verzonden</p>';
} 
else if (substr(sprintf('%o', fileperms('../sessions/'.$withoutExt.'.txt')), -4) == '0775') {
    echo '<p style="color: green;">Verzonden</p>';
}
echo "</td>\n";
    echo "<td>";
$id = $_GET['id'];
$id2 = "../sessions/$withoutExt.txt";
?>
<?php
$filename = $id2;
$fp = fopen($filename, "r");

$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
#if no subusers
if (!strpos($lines['3'], '@') !== false && !strpos($lines['4'], '@') !== false) {
    echo '<p style="color: red;">Geen</p>';
}
if (strpos($lines['3'], '@') !== false) {
    echo 'Subuser 1: ' . $lines['3'] . ' (' . $lines['5'] . ')' . '<br />';
}
if (strpos($lines['4'], '@') !== false) {
    echo 'Subuser 2: ' . $lines['4'] . ' (' . $lines['6'] . ')' . '<br />';
}
?></td><?php echo "\n";
    $filenamerm = $file['name'];
    echo "<td>",date('r', $file['lastmod']),"<br /><a href='rmsess.php?id=$filenamerm' style='color: red;'>Sessie verwijderen</a></td>\n";
    echo "</tr>\n";
  }
  echo "</tbody>\n";
  echo "</table>\n\n";
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
William
  • 427
  • 1
  • 9
  • 22
  • Worth a read: [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](http://meta.stackoverflow.com/q/326569/472495). – halfer Feb 18 '17 at 08:46
  • try this http://stackoverflow.com/questions/11923235/scandir-to-sort-by-date-modified – JYoThI Feb 18 '17 at 08:51
  • 1
    @halfer Agreed, apologies ;) – William Feb 18 '17 at 08:51
  • @JYoThI Already found that, tried integrating it with my code but I wasn't able to integrate it in the code. – William Feb 18 '17 at 08:52

0 Answers0