Now i include all the files in a directory to my site with php using this code:
<?php foreach (glob("overzicht/projects/*.php") as $filename)
{
include $filename;
}
?>
But when the directory is empty I want him to show the text: "there are no files found in the directory."
How do i do this?
$listy = glob("overzicht/projects/*.php");
if (empty($listy)) {
echo "there are no files found in the directory";
} else {
foreach ($listy as $filename) {
include $filename;
}
}
<?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.");
}
?>
I'm sure there's a better way to do this... but that suffices.
Add a flag to detect if a file was found.
<?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.")
}
?>
Grab them as an array, test the array length, and if more than zero, include them, otherwise show the message, as in this example:
<?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;
}
}
?>
$`files = glob("overzicht/projects/*.php"); foreach (`$`files as $filename) { if(file_exist(`$`filename)) { include_once `$`filename; } else { echo 'there are no files found in the directory. '; } } ?> If You have many files in you'r directory. Try this ... I hope it will come up