PHP scandir - multiple directories

2019-08-08 19:55发布

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

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

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

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

1条回答
SAY GOODBYE
2楼-- · 2019-08-08 20:23

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.

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.

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

$files = array_filter($files, function($file){
    return strpos($file, '.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.

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

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.

查看更多
登录 后发表回答