I am trying to sort all the files and folders in a directory using PHP.
I currently can get all the content of the directory (files and folder) and display them on my page.
however, there is absolutely nothing worked for me in terms of sorting the files and folders by the last uploaded/created Time and its driving me up the wall!
This is my entire code:
<?php
$folders = "";
$fileExt = "<i style='font-size:18px;' class='fa fa-folder-open'></i>";
function human_filesize($bytes, $decimals = 2) {
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
$all_doc = "";
$i = 0;
$filesize = "";
$timeFile = "";
if (isset($_GET['dir']) && ($_GET['user'])) {
$directory = $_GET['dir'];
$Uid = $_GET['user'];
//////WE GET THE DIRECTORIES AND FILES HERE///////////////
$dir = "../" . $directory . "";
$dirN = substr($dir, strpos($dir, "/") + 1);
$files = scandir($dir);
function newest($a, $b) {
return (filemtime($a) > filemtime($b)) ? -1 : 1;
}
usort($files, "newest"); // sort the array by calling newest()
$TIMEANDATE = date("F d Y h:i A");
foreach ($files as $file) {
include "../config/connect.php";
$result = $db_conx->query("SELECT * FROM processed WHERE fileName='$file' AND userid='$Uid'");
$row = $result->fetch_array(MYSQLI_BOTH);
$proces = $row["proces"];
$invoice = $row["invoice"];
$date = filemtime('' . $dir . '/' . $file . '');
if ($file != '.' && $file != '..') {
$whatIWant = substr(strrchr($file, '.'), 1);
if ($whatIWant == "jpg" || $whatIWant == "png" || $whatIWant == "gif" || $whatIWant == "JPG" || $whatIWant == "JPEG" || $whatIWant == "jpeg") {
$fileExt = '<i style="font-size:18px;" class="fa fa-file-image-o"></i>';
} else {
$fileExt = "<i style='font-size:18px;' class='fa fa-folder-open'></i>";
}
$filesize = human_filesize(filesize('' . $dir . '/' . $file . ''));
$key = @filemtime('' . $dir . '/' . $file . '');
if ($whatIWant == "pdf" || $whatIWant == "docx" || $whatIWant == "xlsx" || $whatIWant == "xls" || $whatIWant == "zip" || $whatIWant == "doc" || $whatIWant == "jpg" || $whatIWant == "png" || $whatIWant == "gif" || $whatIWant == "txt" || $whatIWant == "psd") {
$timeFile = "" . $dir . "/" . $file . "";
if ($proces == 'yes') {
$all_doc .= "<tr>
<td><input type='checkbox'/></td>
<td>" . $fileExt . " <a class='defaults' href='" . $dir . "/" . $file . "'>" . $file . "</a> <a href='move.php?movef=" . $dirN . "/" . $file . "&user=" . $Uid . "'><i title='Move' class='fa fa-refresh'></i></a>
<a href='folder.php?del=" . $dirN . "/" . $file . "'><i title='Delete' class='fa fa-times'></i>
</a></td>
<td>" . $invoice . "</td>
<td ><a style='color:#0C0;' href='process.php?nopro=" . $file . "&user=" . $Uid . "&loc=" . $dirN . "'>Processed (Click here to un-process)</a></td>
<td>" . date('F d Y h:i A', $date) . "</td>
<td>" . $filesize . "</td>
</td>
</tr>";
} else {
$all_doc .= "<tr>
<td><input type='checkbox'/></td>
<td>" . $fileExt . " <a class='defaults' href='" . $dir . "/" . $file . "'>" . $file . "</a> <a href='move.php?movef=" . $dirN . "/" . $file . "&user=" . $Uid . "'><i title='Move' class='fa fa-refresh'></i></a>
<a href='folder.php?del=" . $dirN . "/" . $file . "'><i title='Delete' class='fa fa-times'></i>
</a></td>
<td>N/A</td>
<td >
<a style='color:#F00;' href='process.php?pro=" . $file . "&user=" . $Uid . "&loc=" . $dirN . "&gback=" . $goBack . "'>Not Processed (Click here to Process)</a>
<!--<form action='process.php' method='get' >
<input type='hidden' name='pro' value='" . $file . "' />
<input type='hidden' name='user' value='" . $Uid . "' />
<input type='hidden' name='loc' value='" . $dirN . "' />
<input class='notPro' type='submit' value='Not Processed (Click here to Process)' />
</form>-->
</td>
<!--<td>" . date('F d Y h:i A', $date) . "</td>-->
<td>" . date("F d Y H:i:s", filemtime($timeFile)) . "</td>
<td>" . $filesize . "</td>
</td>
</tr>";
}
} else {
$all_doc .= "<tr>
<td><input type='checkbox'/></td>
<td>" . $fileExt . " <a class='defaults' href='folder.php?dir=" . $dirN . "/" . $file . "&user=" . $Uid . "'>" . $file . "</a> <a href='move.php?movef=" . $dirN . "/" . $file . "&user=" . $Uid . "'><i title='Move' class='fa fa-refresh'></i></a>
<a href='folder.php?del=" . $dirN . "/" . $file . "'><i title='Delete' class='fa fa-times'></i>
</a></td>
<td>N/A</td>
<td></td>
<td>" . date('F d Y h:i A', $date) . "</td>
<td>" . $filesize . "</td>
</td>
</tr>";
}
$i++;
}
}
}
?>
Sorry about so many code but I have to send you all of it so you know how it works!
as you can see in my code, I am trying to sort the content like so:
$dir = "../".$directory."";
$dirN = substr($dir, strpos($dir, "/") + 1);
$files = scandir($dir);
function newest($a, $b)
{
return (filemtime($a) > filemtime($b)) ? -1 : 1;
}
usort($files, "newest"); // sort the array by calling newest()
but the output of the files and folders is all over the place. Please view the attached image to see how it displays the files and folders at the moment:
Could someone please advise on this issue?
any help would be appreciated.
EDIT:
This question is not sorting by Date
. i need to sort the files by TIME
.