Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 以列表形式读取并显示txt文件的内容_Php - Fatal编程技术网

Php 以列表形式读取并显示txt文件的内容

Php 以列表形式读取并显示txt文件的内容,php,Php,我有一个脚本,列出了文件夹中的所有txt文件。我想做的是显示文件内容而不是文件名。但是我没有成功。。。我尝试了readfile和filegetcontents,但只得到了.txt名称 <?php if ($handle = opendir('../plugins/guestbook/news/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file !

我有一个脚本,列出了文件夹中的所有txt文件。我想做的是显示文件内容而不是文件名。但是我没有成功。。。我尝试了readfile和filegetcontents,但只得到了.txt名称

<?php
if ($handle = opendir('../plugins/guestbook/news/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "<center><label class='tablog3a'>$file <div style='float: right;margin-top:10px;'><a href='gbremove.php?file=".$file."'><img src='images/deletegb.png' title='Verwijder afbeelding' class='clickreverse'></a></div></label></center>";
        }
    }
    closedir($handle);
}
?>
这对我很有用:

<?php 
$dir = "your_directory/"; // Your directory

if ($handle = opendir($dir)) {
  while (false != ($file = readdir($handle))) {
      // Check for .txt files
      if ($file != "." && $file != ".." && substr($file, -3) == "txt") {
        // Open file to read
        $fp = fopen($dir.$file, 'r');

        // Print contents
        echo fread($fp, filesize($dir.$file)).'<br />';
      }
  }
  closedir($handle);
}?>


干杯

你试过
fopen
fread
吗?我试着编辑了我的帖子,但结果却是一片空白lines@Johan旁注:您还可以通过添加
&&&$file!=“exclude_me.txt”
&&$file!=“exclude\u me\u allow.txt”
<?php 
$dir = "your_directory/"; // Your directory

if ($handle = opendir($dir)) {
  while (false != ($file = readdir($handle))) {
      // Check for .txt files
      if ($file != "." && $file != ".." && substr($file, -3) == "txt") {
        // Open file to read
        $fp = fopen($dir.$file, 'r');

        // Print contents
        echo fread($fp, filesize($dir.$file)).'<br />';
      }
  }
  closedir($handle);
}?>