質問

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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top