2015-03-03 77 views
0

嗨,我有PHP,它显示我在dir中的所有文件,并使他们href。但我有编码的麻烦。当我打开文件在web使用这个PHP页面时,编码是错误的。在头部添加元字符集并没有帮助。编码目录中的文件列表

<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

使用'ヶ辆()'来正确编码的一切。 – Barmar 2015-03-03 07:13:50

+0

你能举个例子吗? – dend 2015-03-03 07:40:40

回答

0

使用htmlentities正确编码的一切:

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

谢谢,但它不适合我,试过htmlentities($ file,“windows-1251”) – dend 2015-03-03 08:20:39