How to make directory listing text disappear if directory is empty
-
16-06-2021 - |
题
I'm happy with the php code I'm currently using to display the file contents of a directory, but it currently says "list of files", I would like to know how I could make that text disappear if the directory is empty, if thats possible. The code I am currently using is this:
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$thelist .= '<a href="'.$file.'">'.$file.'</a>';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<P><?=$thelist?></p>
Thank you very much for any help.
解决方案
Just put a condition around the last part:
<?php if (!empty($thelist)) { ?>
<P>List of files:</p>
<P><?=$thelist?></p>
<?php } ?>
其他提示
Try this:
<?php
if ( $handle = opendir('.')) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( $file != "." && $file != ".." ) {
$thelist .= "<a href=\"".$file."\">".$file."</a><br />";
}
}
}
closedir( $handle );
if ( strlen( $thelist ) > 0 ) {
echo "<p>List of files:</p>";
}
?>
<p><?=$thelist?></p>
不隶属于 StackOverflow