如何按时间对目录进行排序,并显示与 php 模式匹配的最后四个图像文件?
我有一个经常更新的目录,我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要最后四个文件,您总是必须进行排序,但您不妨让文件系统为您做这件事。这将在 Linux 上工作,除非您的模式是正则表达式:
如果您必须使用正则表达式,请将前两行更改为
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:
If you have to use a regex, change the first two lines to
不知道你所谓的“对目录中的每个图像执行排序”,但是你必须将所有文件名读入数组,然后读取每个文件的 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.