使用PHP显示文件夹内容,但在文件夹为空时显示消息

num*_*pie 2 html php

我正在为我的工作场所创建一个内部网,并使用了我在网上找到的一些PHP来扫描它所在文件夹的内容并将它们显示为链接.它做得很好,但当它在一个空文件夹中时,我希望它显示一条消息,例如"没有符合这些标准的记录.".

有没有办法在php中添加一些内容来指定是否列出了没有列出的文件夹

我接下来不知道php,但是html和css都没问题.

这是我在页面中使用的php:

<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{
    if ($file != "." and $file != ".." and $file != "A.php")
    {
        array_push($files, $file);
    }
}
closedir($dir);
sort($files);
foreach ($files as $file)
print "<div class='fileicon'>
           <a href='$file'>
               <img src='../../../images/TR-Icon.png'>
               <p class='filetext'>$file</p>
           </a>
      </div>";
?>
Run Code Online (Sandbox Code Playgroud)

如果您需要更多代码,例如整页html或css,请告诉我.

在此先感谢您的帮助.

编辑:

在尝试了Josh的解决方案后,它几乎已经钉了它,但我现在正在"找不到文件"打印3次.这是我现在使用的代码:

<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{   
if( count($files) == 0 )
{
echo '<p>No files found</p>';
}
else
{
if ($file != "." and $file != ".." and $file != "A.php")
{
array_push($files, $file);
}
}
}
closedir($dir);
sort($files);
foreach ($files as $file)
print " <div class='fileicon'>
<a href='$file'>
<img src='../../../images/TR-Icon.png'>
<p class='filetext'>$file</p>
</a>
</div>";
?>
Run Code Online (Sandbox Code Playgroud)

Jos*_*osh 6

做一个:

if( count($files) == 0 )
{
  echo '<p>No files found</p>';
}
else
{
  // you have files
}
Run Code Online (Sandbox Code Playgroud)