PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-如何回显目录中每个文本文件的内容?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

> index.PHP
> one.txt
> two.txt
> three.txt
> four.txt

在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,我想回显目录中每个文件内容(它们都是文本文件).

解决方法:

您可以使用@L_675_30@将文件放入字符串中.

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

此外,您可以使用PHPglob函数仅过滤掉.txt文件,这样,如果您要向该目录添加更多需要忽略的文件,则不必将其列入黑名单.

这是使用glob函数完成的方式.

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

大佬总结

以上是大佬教程为你收集整理的php-如何回显目录中每个文本文件的内容?全部内容,希望文章能够帮你解决php-如何回显目录中每个文本文件的内容?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: