如何排列使用 PHP 显示的文件

发布于 2024-08-05 03:49:08 字数 2516 浏览 5 评论 0原文

<div style="width: 49%; height: 300px; float: left; padding-top: 10px; ">

<h2><img src="downloads/general.png"></h2>

    <h1>General Downloads</h1>
    <?php
    $Dirdownloads = opendir('downloads/general');
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filemtime('downloads/general/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
foreach ($files as $file) {
    echo '<a href="downloads/general/'.$file[1].'">'.$file[1].'</a><br />';
}
 ?>
 </div>

<div style="width: 49%; height: 300px; float: right; padding-top: 10px; ">

<h2><img src="downloads/conference.jpg"></h2>

    <h1>Conference Information</h1>
 <?php
    $Dirdownloads = opendir('downloads/conference');
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filemtime('downloads/conference/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
foreach ($files as $file) {
    echo '<a href="downloads/conference/'.$file[1].'">'.$file[1].'</a><br />';
}
 ?>
 </div>

<div style="width: 49%; height: 300px; float: left; padding-top: 10px; ">

<h2><img src="downloads/newsletter.jpg"></h2>

    <h1>Newsletters</h1>
 <?php
    $Dirdownloads=opendir('downloads/newsletters');
    while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        echo '<a href="downloads/newsletters/'.$file.'">'.$file.'</a><br />';
    }
    }
    closedir($Dirdownloads);
 ?>
 </div>

<div style="width: 49%; height: 300px; float: right; padding-top: 10px; ">

<h2><img src="downloads/calendar.png"></h2> 

<h1>Calendar</h1>
 <?php
    $Dirdownloads=opendir('downloads/calendars');
    while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        echo '<a href="downloads/calendars/'.$file.'">'.$file.'</a><br />';
    }
    }
    closedir($Dirdownloads);
 ?>
 </div>

是代码,它显示某个文件夹中的所有文件,并允许您下载它们。它们目前是按名称排列的,我想以最新的在前排列它们...帮忙?

<div style="width: 49%; height: 300px; float: left; padding-top: 10px; ">

<h2><img src="downloads/general.png"></h2>

    <h1>General Downloads</h1>
    <?php
    $Dirdownloads = opendir('downloads/general');
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filemtime('downloads/general/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
foreach ($files as $file) {
    echo '<a href="downloads/general/'.$file[1].'">'.$file[1].'</a><br />';
}
 ?>
 </div>

<div style="width: 49%; height: 300px; float: right; padding-top: 10px; ">

<h2><img src="downloads/conference.jpg"></h2>

    <h1>Conference Information</h1>
 <?php
    $Dirdownloads = opendir('downloads/conference');
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filemtime('downloads/conference/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
foreach ($files as $file) {
    echo '<a href="downloads/conference/'.$file[1].'">'.$file[1].'</a><br />';
}
 ?>
 </div>

<div style="width: 49%; height: 300px; float: left; padding-top: 10px; ">

<h2><img src="downloads/newsletter.jpg"></h2>

    <h1>Newsletters</h1>
 <?php
    $Dirdownloads=opendir('downloads/newsletters');
    while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        echo '<a href="downloads/newsletters/'.$file.'">'.$file.'</a><br />';
    }
    }
    closedir($Dirdownloads);
 ?>
 </div>

<div style="width: 49%; height: 300px; float: right; padding-top: 10px; ">

<h2><img src="downloads/calendar.png"></h2> 

<h1>Calendar</h1>
 <?php
    $Dirdownloads=opendir('downloads/calendars');
    while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        echo '<a href="downloads/calendars/'.$file.'">'.$file.'</a><br />';
    }
    }
    closedir($Dirdownloads);
 ?>
 </div>

Is the code, which displays all the files in a certain folder, and allows you to download them. They are currently arranged by name, and I want to arrange them with newest first... Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紙鸢 2024-08-12 03:49:08

看看: https://www.php.net/manual/en/function .filemtime.php

基本上组成一个像这样的二维数组:

Array:

[0]File
[0]时间

[1]文件
[1]时间

[2]文件
[2]时间

...

等等,使用filemtime()返回的时间是UNIX格式的。这样您就可以轻松创建一个循环,按“时间”对列表进行排序

Look at: https://www.php.net/manual/en/function.filemtime.php

Basically compose a 2D array like this:

Array:

[0]File
[0]Time

[1]File
[1]Time

[2]File
[2]Time

...

And so on, the time returned using filemtime() is in UNIX format. So then you can easily create a loop which sorts the list by 'Time'

神回复 2024-08-12 03:49:08

将文件收集到数组中,按创建日期排序,然后打印它们:

$dir = 'downloads/general';
$Dirdownloads = opendir($dir);
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filectime($dir.'/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);

// sort files by creation time
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));

// print the files
foreach ($files as $file) {
    echo '<a href="'.$dir.'/'.$file[1].'">'.$file[1].'</a><br />';
}

编辑    当您想要多次使用该算法时,一个函数可能会有所帮助:

function getFilesByCreationDate($dir) {
    $dir = rtrim($dir, '/');
    $handler = opendir($dir);
    while (false !== ($file = readdir($handler))) {
        if ($file != "." && $file != "..") {
            $files[] = array(
                filectime($dir.'/'.$file),
                $file
            );
        }
    }
    closedir($handler);
    usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
    foreach ($files as &$file) {
        $file = $file[1];
    }
    return $files;
}

然后你可以像这样使用这个函数:

$dir = 'downloads/general';
$files = getFilesByCreationDate($dir);
foreach ($files as $file) {
    echo '<a href="'.$dir.'/'.$file.'">'.$file.'</a><br />';
}

Collect the files in an array, sort it by creation date and print them after that:

$dir = 'downloads/general';
$Dirdownloads = opendir($dir);
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filectime($dir.'/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);

// sort files by creation time
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));

// print the files
foreach ($files as $file) {
    echo '<a href="'.$dir.'/'.$file[1].'">'.$file[1].'</a><br />';
}

Edit    As you want to use that algorithms multiple times, a function might be helpful:

function getFilesByCreationDate($dir) {
    $dir = rtrim($dir, '/');
    $handler = opendir($dir);
    while (false !== ($file = readdir($handler))) {
        if ($file != "." && $file != "..") {
            $files[] = array(
                filectime($dir.'/'.$file),
                $file
            );
        }
    }
    closedir($handler);
    usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
    foreach ($files as &$file) {
        $file = $file[1];
    }
    return $files;
}

Then you use that function like this:

$dir = 'downloads/general';
$files = getFilesByCreationDate($dir);
foreach ($files as $file) {
    echo '<a href="'.$dir.'/'.$file.'">'.$file.'</a><br />';
}
仙气飘飘 2024-08-12 03:49:08

如果您的文件列表足够小,您实际上可以将它们读入数组,我要做的就是将它们读入所述数组,然后使用 usort 使用自定义比较函数 filemtime让他们判断哪个文件更新。

usort 的参数是数组本身和一个回调,该回调指定一个函数,该函数本身接收数组的两个元素作为参数,如果第一个参数小于第二个参数,则返回负值,如果它们小于第二个参数,则返回 0相等,如果第一个更大,则为正。

If your list of files is small enough that you can actually read them into an array, what I would do is read them into said array, and then use usort with a custom comparison function that filemtimes them to figure which file is newer.

The arguments to usort are the array itself, and a callback that specifies a function which itself receives two elements of the array as argument, and returns negative if the first argument is less than the second, 0 if they are equal, or positive if the first is greater.

变身佩奇 2024-08-12 03:49:08

您必须:

  • 首先,获取文件列表,其中每个文件的最后修改日期(可以使用 filemtime
  • 然后,使用用户函数对该列表进行排序,该函数根据数组中的“日期”字段进行排序(使用 user-定义的函数,请参见 usort
  • 和,最后,显示列表

例如,首先获取文件列表:

$files = array();

$dir = dirname(__FILE__);
$Dirdownloads=opendir($dir);
while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        $files[] = array(
            'name' => $file,
            'date' => filemtime($dir . '/' . $file),
        );
    }
}
closedir($Dirdownloads);

请注意,对于每个文件,我有一个包含“name”和“date”的数组。

然后,对它们进行排序:

usort($files, 'my_sort_function');

并且,根据“date”字段比较文件的排序函数:

function my_sort_function($a, $b) {
    if ($a['date'] == $b['date']) {
        return 0;
    }
    return ($a['date'] > $b['date']) ? -1 : 1;
}

这样,$file数组应该包含您想要的内容:-)

根据您想要的顺序,您将在排序函数中使用 ><

由您来迭代该列表,以显示链接!

You'll have to :

  • first, get a list of files, with, for each file, its last modification date (this can be fetched using filemtime)
  • then, sort that list, with a user function that sorts depending on the "date" field you've got in you array (to sort using a user-defined function, see usort)
  • and, finally, display the list

For instance, to first get the list of files :

$files = array();

$dir = dirname(__FILE__);
$Dirdownloads=opendir($dir);
while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        $files[] = array(
            'name' => $file,
            'date' => filemtime($dir . '/' . $file),
        );
    }
}
closedir($Dirdownloads);

Note that, for each file, I have an array containing "name" and "date".

And, then, to sort them :

usort($files, 'my_sort_function');

And, the sort function that compares files based on the 'date' field :

function my_sort_function($a, $b) {
    if ($a['date'] == $b['date']) {
        return 0;
    }
    return ($a['date'] > $b['date']) ? -1 : 1;
}

With this, the $file array should contain what you want :-)

Depending on the order you want, you'll use either > or < in the sorting function.

Up to you to iterate over that list, to display the links !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文