0

现在我使用以下代码将目录中的所有文件包含到我的站点的 php 中:

<?php   foreach (glob("overzicht/projects/*.php") as $filename)
{
 include $filename;
}
?>

但是当目录为空时,我希望他显示文本:“目录中没有找到文件。”

我该怎么做呢?

4

5 回答 5

4
$listy = glob("overzicht/projects/*.php");
if (empty($listy)) {
    echo "there are no files found in the directory";
} else {
  foreach ($listy as $filename) {
    include $filename;
  }
}
于 2013-10-10T19:13:38.320 回答
0
<?php
    $flag = true;
    foreach (glob("overzicht/projects/*.php") as $filename)
    {
        include $filename;
        $flag = false;
    }
    if ($flag)
    {
        print("There are no files found in the directory.");
    }
?>

我确信有更好的方法来做到这一点......但这就足够了。

于 2013-10-10T19:13:06.723 回答
0

将它们作为数组抓取,测试数组长度,如果大于零,则包含它们,否则显示消息,如下例所示:

<?php
$files = glob("overzicht/projects/*.php");
if (count($files) == 0)
{
    echo "There are no files found in the directory";
}
else
{
    foreach($files as $file)
    {
        include $file;
    }
}
?>
于 2013-10-10T19:14:45.740 回答
0

$`files = glob("overzicht/projects/*.php"); foreach (`$`files as $filename) { if(file_exist(`$`filename)) { include_once `$`filename; } else { echo '目录下没有找到文件。\n'; } } ?> 如果你的目录中有很多文件。试试这个......我希望它会出现

于 2013-10-10T19:45:07.637 回答
-1

添加标志以检测是否找到文件。

<?php   
$found = false;
foreach (glob("overzicht/projects/*.php") as $filename)
{
    if(file_exists($filename)) {
        include $filename;
        $found = true;
    }
}

if(!$found) {
    print("There are no files found in the directory.")
}
?>
于 2013-10-10T19:13:54.457 回答