参考链接:http://www.runoob.com/php/php-ref-directory.html
<?php
/**
*写一个函数,能够遍历一个文件夹下的所有文件和子文件夹(目录操作)
*
**///利用内置函数 dir 操作目录
//dir(directory,context)函数返回Directory类的实例。该函数用于读取一个目录
//1.给定的要打开的目录
//2.dir()的handle和path两个属性是可用的
//3.handle和path属性有三个方法:read()、rewind()和 close()
function scan_Dir($dir){
$d = dir($dir);
var_dump($d);
echo ‘<br>’;
while(false != ($entry = $d->read())){
echo $entry .’–<br>’;
}
$d->close();
}
echo ‘利用dir函数的结果:’;
scan_Dir(dirname(__FILE__));
echo ‘利用dir函数的结果==end’;
echo ‘<br>’;
echo ‘<br>’;
echo ‘<hr>’;echo ‘利用director函数的scandir()函数’;
var_dump(scandir(dirname(__FILE__)));
echo ‘<br>’;
echo ‘<br>’;
echo ‘<hr>’;/**
* 利用内置函数open_dir打开目录
**/
function scan_Dir2($dir){
$files = array();
if($handle = opendir($dir)){
while (($file = readdir($handle)) !== false) {
if($file != “..” && $file != “.”){
if(is_dir($dir.”/”.$file)){
$files[$file] = scan_Dir2($dir.”/”.$file);
}else{
$files[] = $file;
}
}
}}
closedir($handle); //close
return $files;
}
echo ‘利用director函数的opendir()函数’;
var_dump(scan_Dir2(‘.’));
echo ‘<br>’;
echo ‘<br>’;
print_r(scan_Dir2(“.”));
echo ‘<hr>’;
?>输出结果:
利用dir函数的结果:object(Directory)#1 (2) { [“path”]=> string(20) “F:\phpStudy\WWW\test” [“handle”]=> resource(3) of type (stream) }
.–
..–
dequeue.php–
getpath.php–
index.php–
mongodb.php–
scandir.php–
session–
利用dir函数的结果==end利用director函数的scandir()函数array(8) { [0]=> string(1) “.” [1]=> string(2) “..” [2]=> string(11) “dequeue.php” [3]=> string(11) “getpath.php” [4]=> string(9) “index.php” [5]=> string(11) “mongodb.php” [6]=> string(11) “scandir.php” [7]=> string(7) “session” }
利用director函数的opendir()函数array(6) { [0]=> string(11) “dequeue.php” [1]=> string(11) “getpath.php” [2]=> string(9) “index.php” [3]=> string(11) “mongodb.php” [4]=> string(11) “scandir.php” [“session”]=> array(2) { [“test”]=> array(9) { [“api”]=> array(2) { [0]=> string(6) “uc.php” [1]=> string(14) “uc.php_example” } [0]=> string(9) “close.gif” [“code”]=> array(9) { [0]=> string(10) “avatar.php” [1]=> string(10) “friend.php” [2]=> string(12) “login_db.php” [3]=> string(14) “login_nodb.php” [4]=> string(10) “logout.php” [5]=> string(10) “pmlist.php” [6]=> string(9) “pmwin.php” [7]=> string(15) “register_db.php” [8]=> string(17) “register_nodb.php” } [1]=> string(14) “config.inc.php” [“include”]=> array(1) { [0]=> string(18) “db_mysql.class.php” } [2]=> string(12) “ucexample.js” [3]=> string(15) “ucexample_1.php” [4]=> string(15) “ucexample_2.php” [“uc_client”]=> array(6) { [0]=> string(10) “client.php” [“control”]=> array(10) { [0]=> string(7) “app.php” [1]=> string(9) “cache.php” [2]=> string(10) “domain.php” [3]=> string(8) “feed.php” [4]=> string(10) “friend.php” [5]=> string(9) “index.htm” [6]=> string(8) “mail.php” [7]=> string(6) “pm.php” [8]=> string(7) “tag.php” [9]=> string(8) “user.php” } [“data”]=> array(2) { [“cache”]=> array(3) { [0]=> string(8) “apps.php” [1]=> string(9) “index.htm” [2]=> string(12) “settings.php” } [0]=> string(9) “index.htm” } [1]=> string(9) “index.htm” [“lib”]=> array(5) { [0]=> string(12) “db.class.php” [1]=> string(9) “index.htm” [2]=> string(16) “sendmail.inc.php” [3]=> string(16) “uccode.class.php” [4]=> string(13) “xml.class.php” } [“model”]=> array(12) { [0]=> string(7) “app.php” [1]=> string(8) “base.php” [2]=> string(9) “cache.php” [3]=> string(10) “domain.php” [4]=> string(10) “friend.php” [5]=> string(9) “index.htm” [6]=> string(8) “mail.php” [7]=> string(8) “misc.php” [8]=> string(8) “note.php” [9]=> string(6) “pm.php” [10]=> string(7) “tag.php” [11]=> string(8) “user.php” } } } [“www”]=> array(0) { } } }
Array ( [0] => dequeue.php [1] => getpath.php [2] => index.php [3] => mongodb.php [4] => scandir.php [session] => Array ( [test] => Array ( [api] => Array ( [0] => uc.php [1] => uc.php_example ) [0] => close.gif
=> Array ( [0] => avatar.php [1] => friend.php [2] => login_db.php [3] => login_nodb.php [4] => logout.php [5] => pmlist.php [6] => pmwin.php [7] => register_db.php [8] => register_nodb.php ) [1] => config.inc.php [include] => Array ( [0] => db_mysql.class.php ) [2] => ucexample.js [3] => ucexample_1.php [4] => ucexample_2.php [uc_client] => Array ( [0] => client.php [control] => Array ( [0] => app.php [1] => cache.php [2] => domain.php [3] => feed.php [4] => friend.php [5] => index.htm [6] => mail.php [7] => pm.php [8] => tag.php [9] => user.php ) [data] => Array ( [cache] => Array ( [0] => apps.php [1] => index.htm [2] => settings.php ) [0] => index.htm ) [1] => index.htm [lib] => Array ( [0] => db.class.php [1] => index.htm [2] => sendmail.inc.php [3] => uccode.class.php [4] => xml.class.php ) [model] => Array ( [0] => app.php [1] => base.php [2] => cache.php [3] => domain.php [4] => friend.php [5] => index.htm [6] => mail.php [7] => misc.php [8] => note.php [9] => pm.php [10] => tag.php [11] => user.php ) ) ) [www] => Array ( ) ) )