PHP - 读取dirs的数量,然后要求每个指定的文件名 [英] PHP - Read number of dirs and then require for each a specified file name

查看:217
本文介绍了PHP - 读取dirs的数量,然后要求每个指定的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个小脚本,但我在php中相当n00b,所以请帮助我,如果你可以:)

I want to make a little script but I'm rather n00b in php so please help me if you can :)

我想要脚本做的是


  1. 我有index.php。

  2. 我想要index.php从已知的文件夹中读取子目录,我们假设templates /

  3. 每个子目录将包含一个名为content.html的文件。

  4. 然后,索引将加载每个现有子目录的require()函数的content.html。

  1. I have "index.php".
  2. I want "index.php" to read the sub-directories from a known folder, let's say "templates/"
  3. Each sub-directory will contain a file called "content.html".
  4. The index will then load the "content.html" with the require() function for each of the existing sub-directories.

非常感谢你的帮助!

推荐答案

RecursiveDirectoryIterator 遍历指定的目录和所有子目录,并将所有文件系统项目作为 SplFileInfo 对象:

The RecursiveDirectoryIterator runs through the designated directory and all subdirectories and returns all filesystem items as SplFileInfo objects:

$iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($yourFilePath));

foreach($iterator as $file) {
    if($file->getFilename() === 'content.html') {
        $file->openFile()->fpassthru();
        // or 
        include $file->getRealpath();
    }
}

使用 include 当您要通过PHP解析器运行.html文件时,例如当它包含需要评估的PHP代码。如果没有,请使用fpassthru,因为这应该是更快的分数。也可以使用 readfile($ file-> getRealpath())

Use include when you want to run the .html file through the PHP parser, e.g. when it contains PHP code that needs to be evaluated. If not, use fpassthru, because that should be a fraction faster. Can also use readfile($file->getRealpath()).

这篇关于PHP - 读取dirs的数量,然后要求每个指定的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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