[英]List files from directory with .zip extension and sort by newest created file first
I thought i would share something useful, so i wanted to list only .zip files in a directory (to minimise what i am displaying in PHP for security) so i have used the below script with the newest files on top. 我以为我会分享一些有用的信息,所以我只想在目录中列出.zip文件(以最小化为了安全起见在PHP中显示的内容),所以我在下面的脚本中使用了最新的文件。
This is just some code i wanted to share incase anyone else needed to do something similar. 这只是我想分享的一些代码,以防其他人需要做类似的事情。
<?php
function list_zipfiles($mydirectory, 1) {
// directory we want to scan
$dircontents = scandir($mydirectory);
// list the contents
echo '<ul>';
foreach ($dircontents as $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension == 'zip') {
echo "<li>$file </li>";
}
}
echo '</ul>';
}
?>
<h6 class="header1">PRODUCTION</h6>
<hr class="style1">
<?php
call_user_func('list_zipfiles', "backups/db1");
?>
The 1 below sets the listing order of the files, changing it to 0 orders it in the other direction: 下面的1设置文件的列出顺序,将其更改为0则将文件从另一个方向排序:
function list_zipfiles($mydirectory, 1) {
The output is as below: 输出如下:
请查看我的上述答案,以了解我如何在给定目录中列出所有.zip文件,按文件名排序并用作功能,以便我可以轻松地在其他地方重复使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.