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

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

问题描述

以下代码将列出目录中的所有文件

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

closedir($ handle);
}
?>

< P>文件列表:< / p>
< UL>
< P><?= $ thelist?>< / p>
< / UL>

虽然这是非常简单的代码,但它的工作。



我现在正在寻找一种方法来列出最后只有.xml(或.XML)的文件,我该怎么做?

解决方案

  if($ handle = opendir('。')){
while(false!==($ file = readdir句柄)))
{
if($ file!=。&& $ file!=..&& $ strtolower(substr($ file,strrpos($ file, '。')+ 1))=='xml')
{
$ thelist。='< li>< a href ='。$ file。'>'。$文件。 '< / A>< /立GT;';
}
}
closedir($ handle);
}

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


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.

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);
}

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

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

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