【发布时间】:2015-04-30 10:06:33
【问题描述】:
我正在尝试使用 PHP 对目录中的所有文件和文件夹进行排序。
我目前可以获取目录的所有内容(文件和文件夹)并显示在我的页面上。
但是,在按上次上传/创建的时间对文件和文件夹进行排序方面对我来说绝对没有任何用处,这让我陷入困境!
这是我的全部代码:
<?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++;
}
}
}
?>
很抱歉代码太多,但我必须将所有代码都发送给您,以便您知道它是如何工作的!
正如您在我的代码中看到的,我正在尝试对内容进行如下排序:
$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()
但是文件和文件夹的输出到处都是。请查看附件图片以了解它目前如何显示文件和文件夹:
有人可以就这个问题提出建议吗?
任何帮助将不胜感激。
编辑:
这个问题不是按Date 排序的。我需要按TIME 对文件进行排序。
【问题讨论】:
-
sort files by date in PHP 的可能重复项
-
@JeffSloyer,不,我害怕。这个问题明确要求按
TIME而不是Date排序!但是,在我以任何方式发布我的帖子之前,我已经查看了该帖子。 -
@JeffSloyer,日期和时间是一回事?!?你从哪里得到那个的?不,它没有重复!
-
这是题外话,但您在每次
foreach迭代时连接到 DB,您可能希望将include "../config/connect.php";排除在循环之外 -
我没有查看你所有的代码,但是比较函数'newest'不正确。如果时间相等,它应该返回 0。