ファイル検索
指定フォルダ以下のフォルダ/ファイルを列挙する
lesson001.php
実行結果
<?php
$dir = $argv[1];
$dh = opendir($dir);while ($file = readdir($dh))
{
print "$file\n";
}closedir($dh);
?>
C:\>php c:\study\php\chapter005\lesson001.php c:\study\php
.
..
chapter001
chapter002
chapter003
chapter004
chapter005
ちょいと変更
lesson002.php
実行結果
<?php
putsDir($argv[1]);function putsDir($dir)
{
$dh = opendir($dir);while ($file = readdir($dh))
{
print "$file\n";
}closedir($dh);
}
?>
C:\>php c:\study\php\chapter005\lesson002.php c:\study\php
.
..
chapter001
chapter002
chapter003
chapter004
chapter005
サブフォルダも検索
lesson003.php
実行結果
<?php
putsDir($argv[1]);function putsDir($dir)
{
if (is_dir($dir))
{
$dh = opendir($dir);while ($file = readdir($dh))
{
if ($file == ".") continue; # 自分
if ($file == "..") continue; # 親putsDir("$dir\\$file");
}closedir($dh);
}
else
{
print "$dir\n";
}
}
?>
C:\>php c:\study\php\chapter005\lesson003.php c:\study\php
c:\study\php\chapter001\lesson001.php
c:\study\php\chapter001\lesson002.php
c:\study\php\chapter001\lesson003.php
c:\study\php\chapter001\lesson004.php
c:\study\php\chapter001\lesson005.php
c:\study\php\chapter001\lesson006.php
c:\study\php\chapter001\lesson007.php
c:\study\php\chapter001\lesson008.php
c:\study\php\chapter001\lesson009.php
c:\study\php\chapter001\lesson011.php
c:\study\php\chapter001\php.bat
c:\study\php\chapter001\test.txt
c:\study\php\chapter002\lesson001.php
c:\study\php\chapter002\lesson002.php
c:\study\php\chapter002\lesson003.php
c:\study\php\chapter002\lesson004.php
c:\study\php\chapter002\lesson005.php
c:\study\php\chapter002\lesson006.php
c:\study\php\chapter002\php.bat
c:\study\php\chapter003\lesson001.php
c:\study\php\chapter003\lesson002.php
c:\study\php\chapter003\lesson003.php
c:\study\php\chapter003\lesson004.php
c:\study\php\chapter003\lesson005.php
c:\study\php\chapter003\lesson006.php
c:\study\php\chapter003\lesson007.php
c:\study\php\chapter003\lesson011.php
c:\study\php\chapter003\php.bat
c:\study\php\chapter004\lesson001.php
c:\study\php\chapter004\lesson003.php
c:\study\php\chapter004\lesson004.php
c:\study\php\chapter004\lesson005.php
c:\study\php\chapter004\lesson006.php
c:\study\php\chapter004\lesson007.php
c:\study\php\chapter004\lesson008.php
c:\study\php\chapter004\lesson009.php
c:\study\php\chapter004\lesson010.php
c:\study\php\chapter004\lesson011.php
c:\study\php\chapter004\php.bat
c:\study\php\chapter005\lesson001.php
c:\study\php\chapter005\lesson002.php
c:\study\php\chapter005\lesson003.php
c:\study\php\chapter005\php.bat