PHP scandir-多个目录 [英] PHP scandir - multiple directories

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

问题描述

我正在创建一个WordPress插件,该插件允许用户将排序规则应用于特定模板(页面,归档,单个等).我正在使用PHP scandir填充页面列表,如下所示:

I am creating a WordPress plugin which allows a user to apply sorting rules to a particular template (page, archive, single etc). I am populating list of pages using PHP scandir like so:

$files = scandir(get_template_directory());

问题是我将single.php模板保留在"/single"子文件夹中,因此上述函数没有调用这些模板.

The problem is that I keep single.php templates in a '/single' subfolder so these templates are not being called by the above function.

如何在scandir函数中使用多个目录(也许是数组?),或者我需要其他解决方案?

How can I use multiple directories within the scandir function (perhaps an array?) or will I need a different solution?

所以基本上我正在尝试:

So basically I am trying to:

$files  =   scandir( get_template_directory() AND get_template_directory().'/single' );

我当前的解决方案(不是很优雅,因为每个循环需要2个):

My current solution (not very elegant as it requires 2 for each loops):

        function query_caller_is_template_file_get_template_files()
            {
                $template_files_list    =   array();

                $files          =   scandir(get_template_directory());
                $singlefiles    =   scandir(get_template_directory().'/single');

                foreach($files  as  $file)
                    {
                        if(strpos($file, '.php')    === FALSE)
                            continue;

                        $template_files_list[]  =   $file;
                    }

                foreach($singlefiles  as  $singlefile)
                    {
                        if(strpos($file, '.php')    === FALSE)
                            continue;

                        $template_files_list[]  =   $singlefile;
                    }

                return $template_files_list;
            }

推荐答案

首先,您所做的并不是什么错误.您有两个目录,因此您做两次相同的事情.当然,您可以使其看起来更整洁,并避免使用公然的复制粘贴:

First, there's not really anything wrong about what you're doing. You have two directories, so you do the same thing twice. Of course you could make it look a little cleaner and avoid the blatant copy paste:

$files = array_merge(
    scandir(get_template_directory()),
    scandir(get_template_directory().'/single')
);

现在只需遍历单个数组即可.

Now just iterate over the single array.

在您的情况下,递归获取文件列表没有意义,因为可能有些子目录您不想要检查.如果 did 要递归到子目录,则opendir()readdir()以及is_dir()将允许您构建递归扫描功能.

In your case, getting the file list recursively doesn't make sense, as there may be subdirectories you don't want to check. If you did want to recurse into subdirectories, opendir() and readdir() along with is_dir() would allow you to build a recursive scan function.

您可以通过array_filter()稍微拧紧'.php'过滤器部分.

You could event tighten up the '.php' filter part a bit with array_filter().

$files = array_filter($files, function($file){
    return strpos($file, '.php');
});

在这里,我假设文件应该以.php开头,而您对创建列表并不真正感兴趣(因为strpos()在这种情况下将返回伪造的0值).我还假设您确定中间没有.php的文件.

Here I'm assuming that should a file start with .php you're not really interested in it making your list (as strpos() will return the falsy value of 0 in that case). I'm also assuming that you're sure there will be no files that have .php in the middle somewhere.

就像template.php.bak,因为您将使用类似这样的版本控制功能.

Like, template.php.bak, because you'll be using version control for something like that.

但是,如果有机会,您可能需要加紧检查,以确保.php位于文件名的 end 处.

If however there is the chance of that, you may want to tighten up your check a bit to ensure the .php is at the end of the filename.

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

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