简体   繁体   English

PHP按日期/时间和文件大小对文件排序

[英]PHP Sorting files by Date/time and file size

I am working on modifying a simple website. 我正在修改一个简单的网站。 This website has a page that shows a clients files available for download(if applicable). 该网站的页面显示了可供下载的客户端文件(如果适用)。 At the moment these files are just in random order with no specific details. 目前,这些文件只是随机排列的,没有具体细节。 I would like to be able to have them in decending order based on time stamp that they were made available. 我希望能够根据提供它们的时间戳以降序排列它们。 Also including their file size. 还包括其文件大小。 This is using php to show the files, do I need to have the directory already sorted before displaying them? 这是使用php显示文件,在显示它们之前是否需要对目录进行排序? if so would that be a separate script and when would it be run? 如果是这样,那将是一个单独的脚本,什么时候运行? Or can I sort them as they are displayed in the follwoing code? 还是可以按照以下代码中的显示对它们进行排序?

<div id="f2">
<h3>Files Available for Download</h3>
<p>
<?php
// list contents of user directory
if (file_exists($USER_DIRECTORY)) {
    if ($handle = opendir($USER_DIRECTORY)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                echo "<a href='download_file.php?filename=".urlencode($entry)."'>".$entry."</a><br/>";
            }
        }
    closedir($handle);
    }
}

?>
</p>

</div>

Very new to php, so any help is appreciated. PHP的新手,因此不胜感激。

Here's a snippet which could be of help. 这是一个片段,可能会有所帮助。

It makes an href out of each file or file extension specified. 它从指定的每个文件或文件扩展名中提取href Modify to suit. 修改以适合。

It can easily be changed to usort() . 可以很容易地将其更改为usort() Consult the PHP manual. 查阅PHP手册。

See also the arsort() function. 另请参见arsort()函数。 Consult the PHP manual. 查阅PHP手册。

The filesize is also included, yet it is not formatted as bytes, kb etc. There are functions out there that will format it to suit. 文件大小也包括在内,但未将其格式化为字节,kb等。有一些函数可以对其进行格式化以适应需要。 Google "filesize format php". Google “文件大小格式php”。 This link has that information. 该链接包含该信息。

<?php

// You can use the desired folder to check and comment the others.
// foreach (glob("../downloads/*") as $path) { // lists all files in sub-folder called "downloads"
foreach (glob("test/*") as $path) { // lists all files in folder called "test"
//foreach (glob("*.php") as $path) { // lists all files with .php extension in current folder
    $docs[$path] = filectime($path);
} asort($docs); // sort by value, preserving keys

foreach ($docs as $path => $timestamp) {
    print date("d M. Y: ", $timestamp);
    print '<a href="'. $path .'">'. basename($path) .'</a>' . " Size: " . filesize($path) .'<br />';
}
?>

Pulled from that link http://codebyte.dev7studios.com/post/1590919646/php-format-filesize , should it ever cease to exist: 如果该链接不复存在,则从该链接http://codebyte.dev7studios.com/post/1590919646/php-format-filesize提取:

function filesize_format($size, $sizes = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'))
{
    if ($size == 0) return('n/a');
    return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $sizes[$i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM