目录中特定文件的 PHP 列表 [英] PHP list of specific files in a directory

查看:30
本文介绍了目录中特定文件的 PHP 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码会列出一个目录下的所有文件

The following code will list all the file in a directory

<?php
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle)))
    {
        if (($file != ".") 
         && ($file != ".."))
        {
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
        }
    }

    closedir($handle);
}
?>

<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>

虽然这是非常简单的代码,但它可以完成工作.

While this is very simple code it does the job.

我现在正在寻找一种方法来仅列出结尾为 .xml(或 .XML)的文件,我该怎么做?

I'm now looking for a way to list ONLY files that have .xml (or .XML) at the end, how do I do that?

推荐答案

if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'xml')
        {
            $thelist .= '<li><a href="'.$file.'">'.$file.'</a></li>';
        }
    }
    closedir($handle);
}

使用 substr 和 strrpos 查看扩展的简单方法

A simple way to look at the extension using substr and strrpos

这篇关于目录中特定文件的 PHP 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆