2013年4月12日金曜日

PHPでマルチバイトを含んだディレクトリのリストアップ

PHPでディレクトリをリストアップして、aタグでパラメータとして渡せるようにしてみる。
<?php

$dir = "/hoge/fuga";
if (isset($_REQUEST["dir"]) && $_REQUEST["dir"]) {
  $dir = mb_convert_encoding($_REQUEST["dir"], "SJIS", "UTF-8");
}
$html = "";

if ($dh = opendir($dir)) {
  while ($entry = readdir($dh)) {
    if (is_dir($dir . DIRECTORY_SEPARATOR . $entry) && $entry !== "." && $entry !== "..") {
      $html .= 
        "<a href=\"dir.php?dir=" . urlencode(mb_convert_encoding($entry, "UTF-8", "SJIS")) . "\">" . 
        mb_convert_encoding($entry, , "UTF-8", "SJIS") . 
        "</a><br />";
    }
  }
}

?><html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
    <?php echo $html; ?>
  </body>
</html>

0 件のコメント:

コメントを投稿