现在我使用以下代码将目录中的所有文件包含到我的网站:
<?php foreach (glob("overzicht/projects/*.php") as $filename)
{
include $filename;
}
?>
但是当目录为空时我希望他显示文本:“目录中没有找到文件。”
我该怎么做?
答案 0 :(得分: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;
}
}
答案 1 :(得分: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.");
}
?>
我确信有更好的方法可以做到这一点......但这就足够了。
答案 2 :(得分: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;
}
}
?>
答案 3 :(得分:0)
$`files = glob(“overzicht / projects / * .php”); foreach(`$`files as $ filename){ if(file_exist(`$`filename)){ include_once` $`filename; } else { echo'目录中没有找到文件。\ n'; } } ?&GT; 如果你的目录中有很多文件。试试这个...我希望它会出现
答案 4 :(得分:-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.")
}
?>