如何回显目录中每个文本文件的内容?

How to echo out the contents of every text file that in a directory?

我创建了一个目录,其中包含一些文件:

index.php 页面中,我目前正在使用此代码回显目录中的所有文件:

<?php
$blacklist = array("index.php");

if ($handle = opendir('.')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != ".." &&  !in_array($entry, $blacklist)) {
            echo "$entry\n";
        }

    }

    closedir($handle);
}
?>

现在,如果有人查看 index.php 页面,他们将看到以下内容:

one.txt two.txt three.txt four.txt

正如您从 PHP 代码中看到的那样,index.php 已被列入黑名单,因此不会被回显。

但是,我想更进一步,回显每个文本文件的内容而不是文件名。使用新的 PHP 代码(我需要帮助创建),每当有人访问 index.php 页面时,他们现在会看到:

(请忽略星号中的内容,它们不是代码的一部分,它们只是表示每个文本文件包含的内容)

Hello  ** this is what the file **one.txt** contains **

ok  ** this is what the file **two.txt** contains **

goodbye  ** this is what the file **three.txt** contains **

text  ** this is what the file **four.txt** contains **

总体:

除了index.php.

,我想回显目录中每个文件的内容(它们都是文本文件)

您可以使用 file_get_contents 将文件放入字符串中。

<?php
   $blacklist = array("index.php");

   if ($handle = opendir('.')) {

      while (false !== ($entry = readdir($handle))) {

          if ($entry != "." && $entry != ".." &&  !in_array($entry, $blacklist)) {
              echo "$entry " . file_get_contents($entry) . "\n";
          }

       }

      closedir($handle);
   }
?>

此外,您可以使用 PHP 的 glob 功能仅过滤掉 .txt 文件,这样您就不必将文件列入黑名单向该目录添加更多需要忽略的文件。

下面是使用 glob 函数的方法。

<?php
   foreach (glob("*.txt") as $filename) {
      echo "$filename " . file_get_contents($filename) . "\n";
   }
?>

这将打印文件的内容。如果路径不是当前路径并在文件内容之间写入某种边界,您可以采取一些解决方法。

<?php
$blacklist = array("index.php");

if ($handle = opendir('.')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != ".." &&  !in_array($entry, $blacklist)) {
            echo  file_get_contents($entry) . "\n";
        }

    }

    closedir($handle);
}
?>

希望对你有所帮助。

如@brock-b 所说,您可以使用 glob to get the full list of files and file_get_contents 获取内容:

$blacklist = array('index.php');

$files = glob('*.txt'); # could be *.* if needed
foreach ($files as $file) {
    if (!in_array(basename($file), $blacklist)) {
        echo file_get_contents($file);
    }
}

注意:黑名单不会被命中,因为你正在寻找*.txt个文件。仅在执行 *.**.php 文件搜索时有用

永远不要重新发明轮子。使用作曲家。

需要symfony/finder

use Symfony\Component\Finder\Finder;

class Foo
{
    public function getTextFileContents($dir)
    {
        $finder = (new Finder())->files()->name('*.txt');
        foreach ($finder->in($dir) as $file) {
            $contents = $file->getContents();
            // do something while file contents...
        }
    }
}

我会给一些 SPL filesystem iterators 机会来完成这样的任务:

$dir = '/home/mydirectory';
$rdi = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
$rdi = new \RegexIterator($rdi, '/\.txt$/i');
$iterator = new \RecursiveIteratorIterator($rdi, \RecursiveIteratorIterator::CHILD_FIRST);

foreach ($iterator as $file) {
    echo 'Contents of the '.$file->getPathname().' is: ';
    echo file_get_contents($file->getPathname());
}

这将递归查找并迭代给定目录中的所有 .txt 文件,包括子目录。

由于迭代中的每个 $file 都是一个 FilesystemIterator 实例,您可以使用 all related methods 进行其他控制,例如 $file->isLink()(符号链接为真),$file->isReadable()(对于不可读文件为 false)等..

如果您不想查找子文件夹,只需将第二行中的 RecursiveDirectoryIterator 更改为:

$rdi = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);

至:

$rdi = new \DirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);

希望对您有所帮助。