如何按时间对目录进行排序,并显示与 php 模式匹配的最后四个图像文件?

发布于 2024-08-26 07:58:56 字数 100 浏览 2 评论 0原文

我有一个经常更新的目录,我想在我的 php 页面上显示最后上传的四个图像文件,它们与特定模式匹配。

这是否可能无需对目录中的每个图像执行排序?

非常感谢!

I have a directory that will be getting updated frequently, I want to display the last four images files uploaded, which match a certain pattern, on my php page.

Is this possible without having to perform a sort on every single image within the directory?

Many thanks!

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

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

发布评论

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

评论(2

私藏温柔 2024-09-02 07:58:56

如果您想要最后四个文件,您总是必须进行排序,但您不妨让文件系统为您做这件事。这将在 Linux 上工作,除非您的模式是正则表达式:

$pattern = '*.jpg';
exec( "ls $pattern -1t | head -4", $files );
foreach( $files as $thisFile ) {
    echo "<img src='", $serverPathToFiles, $thisFile, "' alt='blah' />\n";
}

如果您必须使用正则表达式,请将前两行更改为

$pattern = 'web2\.[4-5]'; // for web2.42 etc.
exec( "ls -1t | grep -e $pattern | head -4", $files );

You'll always have to sort if you want the last four files, but you might as well let the filesystem do it for you. This will work on linux unless your pattern's a regex:

$pattern = '*.jpg';
exec( "ls $pattern -1t | head -4", $files );
foreach( $files as $thisFile ) {
    echo "<img src='", $serverPathToFiles, $thisFile, "' alt='blah' />\n";
}

If you have to use a regex, change the first two lines to

$pattern = 'web2\.[4-5]'; // for web2.42 etc.
exec( "ls -1t | grep -e $pattern | head -4", $files );
泛泛之交 2024-09-02 07:58:56

不知道你所谓的“对目录中的每个图像执行排序”,但是你必须将所有文件名读入数组,然后读取每个文件的 ctime 。然后对结果数组进行排序。

Dunno what do you call "to perform a sort on every single image within the directory", but you have to read all file names into array and then read ctime for the every file. and then sort the resulting array.

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