如何排列使用 PHP 显示的文件
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看: 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'
将文件收集到数组中,按创建日期排序,然后打印它们:
编辑 当您想要多次使用该算法时,一个函数可能会有所帮助:
然后你可以像这样使用这个函数:
Collect the files in an array, sort it by creation date and print them after that:
Edit As you want to use that algorithms multiple times, a function might be helpful:
Then you use that function like this:
如果您的文件列表足够小,您实际上可以将它们读入数组,我要做的就是将它们读入所述数组,然后使用
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 thatfilemtime
s 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.您必须:
filemtime
)usort
)例如,首先获取文件列表:
请注意,对于每个文件,我有一个包含“
name
”和“date
”的数组。然后,对它们进行排序:
并且,根据“
date
”字段比较文件的排序函数:这样,
$file
数组应该包含您想要的内容:-)根据您想要的顺序,您将在排序函数中使用
>
或<
。由您来迭代该列表,以显示链接!
You'll have to :
filemtime
)usort
)For instance, to first get the list of files :
Note that, for each file, I have an array containing "
name
" and "date
".And, then, to sort them :
And, the sort function that compares files based on the '
date
' field :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 !