编码目录中的文件列表

时间:2015-03-03 07:10:36

标签: php html

嗨我有php,它显示我在dir中的所有文件并使它们成为href。但我在编码方面遇到了麻烦。当我在这个php页面中打开web文件时,编码是错误的。在头脑中添加meta charset并没有帮助。

<html>
    <head>
    <meta http-equiv="Content-Type" content="application/html; charset=utf-8" />
    </head>
    <body>
    <?php
     if ($handle = opendir('/opt/nagios/share/kpi_backup')) {
       while (false !== ($file = readdir($handle)))
          {
              if ($file != "." && $file != "..")
              {
                    $thelist .= '<a href="'.$file.'">'.$file.'</a>';
              }
           }
      closedir($handle);
      }

    ?>
    <p>List of files:</p>
    <p><?=$thelist?></p>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

使用htmlentities对所有内容进行正确编码:

$thelist .= '<a href="'.htmlentities($file) .'">'.htmlentities($file).'</a>';