阅读<li> </li>中的Dir PHP / Echo文件内容

时间:2013-12-19 02:52:20

标签: php include directory

简而言之:我试图为一个能够读取目录.txt文件的网站制作最新的新闻滚动条,然后在'li'标签内显示它们的内容。允许我添加一个文件管理器,我可以在其中上传多个文本文件,并在滚动条脚本中正确显示它们。

长度:

我使用此脚本来读取和显示目录中的文件:

<?php
$dir = 'latest';
$files = scandir($dir);
foreach($files as $ind_file){
?>
<li><a href="<?php echo $dir."/".$ind_file;?>"><?php echo $ind_file;?></li>
<?php
}
?>

但是只显示目录中的.txt文件列表。

我想在各个'li'标签中阅读和显示.txt文件的内容,这与php include的方式非常相似:

<?php include ('file.txt'); ?>

需要此格式的原因是在this脚本中使用。

我不太了解php,因为我尝试过的所有内容都失败了,所以如果有人知道如何正确地写这个或知道更好的方法来实现这一点,我会对所有想法开放。

1 个答案:

答案 0 :(得分:1)

试试这个:

$dir = 'latest';
foreach (glob("$dir/*.txt") as $filename) {
    $contents = file_get_contents($filename);
    echo "<li>$contents</li>";      
}

检查glob documentation以根据您的需要采用此脚本。