简体   繁体   English

PHP文件:文件夹AND子文件夹中的最新11个文件

[英]PHP file: most recent 11 files in folder AND subfolders

I have a directory with about 100 sub-directories (nested, some 3-4 deep). 我有一个包含大约100个子目录的目录(嵌套,大约3-4个深度)。 I need a small PHP script that will simply output a list of the 11 most recently added (or modified, as they are all pdf's) files, with a simple <br /> between each, each being linked to their URL. 我需要一个小的PHP脚本,该脚本将仅输出最近添加的11个(或已修改,因为它们都是pdf的)文件的列表,每个文件之间都有一个简单的<br /> ,每个文件都链接到其URL。

I have found a few scripts that can do 1, or all, but have no clue how to even begin to limit to 11 specifically, or how to apply the exact href. 我发现一些脚本可以执行1个或全部操作,但是不知道如何开始专门限制到11个脚本,或者如何应用确切的href。

my alternate concern is server usage of this script, as it will be placed on a landing page that will see low usage, however every user will see it. 我的另一个担心是此脚本在服务器上的使用情况,因为它将放置在使用率较低的登录页面上,但是每个用户都会看到它。

any ideas on resources to look into, or places to start, would be appreciated! 任何有关资源探索或起点的想法,将不胜感激!

Not tested, but it should work: 未经测试,但应该可以工作:

<?php

function getFiles($dir)
{
    $arr=scandir($dir);
    $res=array();
    foreach ($arr as $item)
    {
        if ($item=='..' || $item=='.'){continue;}
        $path=$dir.'/'.$item;
        if (is_dir($path))
        {
            $res=array_merge($res,getFiles($path));
        }
        else
        {
            $res[$path]=filemtime($path);
        }
    }
    return $res;
}

$files=getFiles('directory');
arsort($files);
$files=array_slice(array_keys($files),0,11);
foreach ($files as $file)
{
    echo '<a href="'.$file.'">'.$file.'</a><br />';
}
?>

It would make more sense to provide a web-based upload interface. 提供一个基于Web的上传界面会更有意义。 That way you would be able to keep the track of each new upload. 这样,您就可以跟踪每个新上传的内容。

  • user chooses category (each category/subcategory maps to a folder) 用户选择类别(每个类别/子类别映射到一个文件夹)
  • upload file(s) to server 将文件上传到服务器
  • script dump each filename, location and date in table row 脚本转储表行中的每个文件名,位置和日期

Done. 做完了 To get latest 11 file (in entire system or specific category/folder)you need only query database. 要获取最新的11个文件(在整个系统或特定类别/文件夹中),您只需查询数据库。

If your users need the ability to remove files too, just add web interface for that functionality too. 如果您的用户也需要删除文件的能力,则只需为该功能添加Web界面。 And instead of deleting the file, just mark them as "hidden" in the table. 而不是删除文件,只需在表中将其标记为“隐藏”即可。

Besides solving the immediate problem, it will also provide additional possibilities for moderation, attribution of ownership and search .. and many more. 除了解决眼前的问题,它还将为审核,所有权归属和搜索提供更多的可能性。

Use a RecursiveIterator. 使用RecursiveIterator。 This is what it was designed for. 这就是它的设计目的。 I added an extra iterator named sortedIterator which extends SPLHeap, to sort by getMTime(), which is the modified time of the dir. 我添加了一个名为sortedIterator的额外迭代器,该迭代器扩展了SPLHeap,以按getMTim​​e()进行排序,该时间是dir的修改时间。

Change $path to be your full path to the directory. 将$ path更改为目录的完整路径。

$path = '.';

class sortedIterator extends SplHeap
{
    public function __construct(Iterator $iterator)
    {
        foreach ($iterator as $item) {
            $this->insert($item);
        }
    }
    public function compare($b,$a)
    {
        return strcmp($a->getMTime(), $b->getMTime());
    }
}

    $recursive = new RecursiveDirectoryIterator($path);
$sortByTime = new sortedIterator($recursive);
$i = 0;
foreach ($sortByTime as $file) {
        if($file->isDir()){
                $dirs[] = $file->getFilename();

                if($i++ == 9){
                        break;
                }
        }
}

print_r(implode("<br />", $dirs));

Edit, if you want Created Time, change getMTime to getCTime. 编辑,如果要创建时间,请将getMTim​​e更改为getCTime。

Use the code that lists all of them, but before outputting use a usort call to sort the files by decreasing modification time. 使用列出所有文件的代码,但是在输出之前,请使用usort调用通过减少修改时间来对文件进行排序。 Then, your loop should start with this: 然后,您的循环应以此开始:

$l = count($fileArray);
for( $i=0; $i<11 && $i<$l; $i++) {
    // output file entry
}

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

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