コードを通して PHP ファイルの関数をまとめてみましょう
// current directoryecho getcwd() . "\n";chdir('hhh');// current directoryecho getcwd() . "\n";
D:xampphtdocsmyfunction
D:xampphtdocsmyfunctionhh
$dir = 'D:\xampp\htdocs\my\function';// Open a known directory, read directory into variable and then closeif (is_dir($dir)) { if ($dh = opendir($dir)) { $ds = array(); while ($directory = readdir($dh)) { $ds[] = $directory; } rewinddir($dh); while ($directory = readdir($dh)) { $ds[] = $directory; } closedir($dh); }}print_r($ds);
Array ( [0] => . [1] => . .[2] = > file.php [3] => ああ [4] => .. [6] => ああ
Array ( [0] => haha [1] => file.php [2] => .. [3] => . )
$dir = 'D:\xampp\htdocs\my\function';showfiles3($dir);function showfiles1($dir){ if (is_dir($dir)) { echo "<ul>"; $dh = opendir($dir); while ($file = readdir($dh)) { if (is_dir($dir.DIRECTORY_SEPARATOR.$file)) { if ($file !='.' && $file != '..') { echo "<li>"; echo "|-".$file; showfiles1($dir.DIRECTORY_SEPARATOR.$file); echo "</li>"; } } else{ echo "<li>"; echo "--".$file; echo "</li>"; } } closedir($dh); echo "</ul>"; }}function showfiles2($dir){ echo "<ul>"; $arr = scandir($dir); foreach ($arr as $file) { if (is_dir($dir.DIRECTORY_SEPARATOR.$file)) { if ($file !='.' && $file != '..') { echo "<li>"; echo "|-".$file; showfiles2($dir.DIRECTORY_SEPARATOR.$file); echo "</li>"; } } else{ echo "<li>"; echo "--".$file; echo "</li>"; } } echo "</ul>";}function showfiles3($dir) { foreach (glob($dir.DIRECTORY_SEPARATOR.'*') as $f) { echo is_dir($f) ? showfiles3($f) : "$f<br />"; }}
ちょっとした質問: .file と ..ファイル
の詳細な分析