PHP遍历文件夹及子文件夹所有文件

 1 <html>
 2     <body>
 3         <?php
 4             function traverse($path = '.') {
 5                 $current_dir = opendir($path);    //opendir()返回一个目录句柄,失败返回false
 6                 while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
 7                     $sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
 8                     if($file == '.' || $file == '..') {
 9                         continue;
10                     } else if(is_dir($sub_dir)) {    //如果是目录,进行递归
11                         echo 'Directory ' . $file . ':<br>';
12                         traverse($sub_dir);
13                     } else {    //如果是文件,直接输出
14                         echo 'File in Directory ' . $path . ': ' . $file . '<br>';
15                     }
16                 }
17             }
18             
19             traverse('xxtt');
20         ?>
21     </body>
22 </html>

页面输出

Directory autoload:
File in Directory d:\www\autoload: MyClass.php
File in Directory d:\www\autoload: MyClass2.php
File in Directory d:\www\autoload: test.php
File in Directory d:\www: changelog.txt
File in Directory d:\www: cryptForm.php
File in Directory d:\www: diffDomain.php
Directory ExamingOnline:
Directory New folder:
File in Directory d:\www: example.php
File in Directory d:\www: example2.php
Directory Excel:
File in Directory d:\www\Excel: oleread.inc
File in Directory d:\www\Excel: oleread.php
File in Directory d:\www\Excel: reader.php
File in Directory d:\www: expert.xls
File in Directory d:\www: expert.xlsx
File in Directory d:\www: index.php
File in Directory d:\www: index2.php
File in Directory d:\www: jxlrwtest.xls
File in Directory d:\www: lcs.php
Directory listFile:
File in Directory d:\www\listFile: listFile.php
File in Directory d:\www: nQueens.php
Directory proxypass:
Directory proxy:
File in Directory d:\www\proxypass\proxy: login.php
File in Directory d:\www\proxypass\proxy: result.php
File in Directory d:\www\proxypass: success.php
File in Directory d:\www: README
File in Directory d:\www: somefile0.php
Directory Study:
Directory .idea:
File in Directory d:\www\Study\.idea: .name
File in Directory d:\www\Study\.idea: encodings.xml
File in Directory d:\www\Study\.idea: misc.xml
File in Directory d:\www\Study\.idea: modules.xml
File in Directory d:\www\Study\.idea: Study.iml
File in Directory d:\www\Study\.idea: vcs.xml
File in Directory d:\www\Study\.idea: workspace.xml
File in Directory d:\www\Study: NSquare.php
File in Directory d:\www\Study: regular.php
Directory templates:
File in Directory d:\www\Study\templates: tpl.php
Directory templates_c:
File in Directory d:\www\Study\templates_c: com_tpl.html.php
File in Directory d:\www\Study\templates_c: com_tpl.php.php
File in Directory d:\www\Study: test.php
Directory sub:
File in Directory d:\www\sub: index.php
Directory template:
File in Directory d:\www\template: template.php
Directory templates:
File in Directory d:\www\template\templates: tpl.html
Directory templates_c:
File in Directory d:\www\template\templates_c: com_tpl.html.php
Directory test:
Directory adodb5:
File in Directory d:\www\test\adodb5: adodb-active-record.inc.php
File in Directory d:\www\test\adodb5: adodb-active-recordx.inc.php
File in Directory d:\www\test\adodb5: adodb-csvlib.inc.php
File in Directory d:\www\test\adodb5: adodb-datadict.inc.php
File in Directory d:\www\test\adodb5: adodb-error.inc.php
File in Directory d:\www\test\adodb5: adodb-errorhandler.inc.php
File in Directory d:\www\test\adodb5: adodb-errorpear.inc.php
File in Directory d:\www\test\adodb5: adodb-exceptions.inc.php
File in Directory d:\www\test\adodb5: adodb-iterator.inc.php
File in Directory d:\www\test\adodb5: adodb-lib.inc.php
File in Directory d:\www\test\adodb5: adodb-memcache.lib.inc.php
File in Directory d:\www\test\adodb5: adodb-pager.inc.php
File in Directory d:\www\test\adodb5: adodb-pear.inc.php
File in Directory d:\www\test\adodb5: adodb-perf.inc.php
File in Directory d:\www\test\adodb5: adodb-php4.inc.php
File in Directory d:\www\test\adodb5: adodb-time.inc.php
File in Directory d:\www\test\adodb5: adodb-xmlschema.inc.php
File in Directory d:\www\test\adodb5: adodb-xmlschema03.inc.php
File in Directory d:\www\test\adodb5: adodb.inc.php
Directory contrib:
File in Directory d:\www\test\adodb5\contrib: toxmlrpc.inc.php
Directory cute_icons_for_site:
File in Directory d:\www\test\adodb5\cute_icons_for_site: adodb.gif
File in Directory d:\www\test\adodb5\cute_icons_for_site: adodb2.gif
Directory datadict:
File in Directory d:\www\test\adodb5\datadict: datadict-access.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-db2.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-firebird.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-generic.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-ibase.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-informix.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-mssql.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-mssqlnative.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-mysql.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-oci8.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-postgres.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-sapdb.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-sqlite.inc.php
File in Directory d:\www\test\adodb5\datadict: datadict-sybase.inc.php
Directory docs:
File in Directory d:\www\test\adodb5\docs: docs-active-record.htm
File in Directory d:\www\test\adodb5\docs: docs-adodb.htm
File in Directory d:\www\test\adodb5\docs: docs-datadict.htm
File in Directory d:\www\test\adodb5\docs: docs-oracle.htm
File in Directory d:\www\test\adodb5\docs: docs-perf.htm
File in Directory d:\www\test\adodb5\docs: docs-session.htm
File in Directory d:\www\test\adodb5\docs: docs-session.old.htm
File in Directory d:\www\test\adodb5\docs: old-changelog.htm
File in Directory d:\www\test\adodb5\docs: readme.htm
File in Directory d:\www\test\adodb5\docs: tips_portable_sql.htm
File in Directory d:\www\test\adodb5\docs: tute.htm
Directory drivers:
File in Directory d:\www\test\adodb5\drivers: adodb-access.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-ado.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-ado5.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-ado_access.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-ado_mssql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-ads.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-borland_ibase.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-csv.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-db2.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-db2oci.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-db2ora.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-fbsql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-firebird.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-ibase.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-informix.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-informix72.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-ldap.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mssql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mssqlnative.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mssqlpo.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mssql_n.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mysql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mysqli.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mysqlpo.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-mysqlt.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-netezza.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-oci8.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-oci805.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-oci8po.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-odbc.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-odbc_db2.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-odbc_mssql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-odbc_oracle.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-odbtp.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-odbtp_unicode.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-oracle.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-pdo.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-pdo_mssql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-pdo_mysql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-pdo_oci.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-pdo_pgsql.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-pdo_sqlite.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-postgres.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-postgres64.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-postgres7.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-postgres8.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-proxy.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-sapdb.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-sqlanywhere.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-sqlite.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-sqlite3.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-sqlitepo.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-sybase.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-sybase_ase.inc.php
File in Directory d:\www\test\adodb5\drivers: adodb-vfp.inc.php
Directory lang:
File in Directory d:\www\test\adodb5\lang: adodb-ar.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-bg.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-bgutf8.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-ca.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-cn.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-cz.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-da.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-de.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-en.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-es.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-esperanto.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-fa.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-fr.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-hu.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-it.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-nl.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-pl.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-pt-br.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-ro.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-ru1251.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-sv.inc.php
File in Directory d:\www\test\adodb5\lang: adodb-uk1251.inc.php
File in Directory d:\www\test\adodb5\lang: adodb_th.inc.php
File in Directory d:\www\test\adodb5: license.txt
Directory pear:
Directory Auth:
Directory Container:
File in Directory d:\www\test\adodb5\pear\Auth\Container: ADOdb.php
File in Directory d:\www\test\adodb5\pear: readme.Auth.txt
Directory perf:
File in Directory d:\www\test\adodb5\perf: perf-db2.inc.php
File in Directory d:\www\test\adodb5\perf: perf-informix.inc.php
File in Directory d:\www\test\adodb5\perf: perf-mssql.inc.php
File in Directory d:\www\test\adodb5\perf: perf-mssqlnative.inc.php
File in Directory d:\www\test\adodb5\perf: perf-mysql.inc.php
File in Directory d:\www\test\adodb5\perf: perf-oci8.inc.php
File in Directory d:\www\test\adodb5\perf: perf-postgres.inc.php
File in Directory d:\www\test\adodb5: pivottable.inc.php
File in Directory d:\www\test\adodb5: readme.txt
File in Directory d:\www\test\adodb5: rsfilter.inc.php
File in Directory d:\www\test\adodb5: server.php
Directory session:
File in Directory d:\www\test\adodb5\session: adodb-compress-bzip2.php
File in Directory d:\www\test\adodb5\session: adodb-compress-gzip.php
File in Directory d:\www\test\adodb5\session: adodb-cryptsession.php
File in Directory d:\www\test\adodb5\session: adodb-cryptsession2.php
File in Directory d:\www\test\adodb5\session: adodb-encrypt-mcrypt.php
File in Directory d:\www\test\adodb5\session: adodb-encrypt-md5.php
File in Directory d:\www\test\adodb5\session: adodb-encrypt-secret.php
File in Directory d:\www\test\adodb5\session: adodb-encrypt-sha1.php
File in Directory d:\www\test\adodb5\session: adodb-sess.txt
File in Directory d:\www\test\adodb5\session: adodb-session-clob.php
File in Directory d:\www\test\adodb5\session: adodb-session-clob2.php
File in Directory d:\www\test\adodb5\session: adodb-session.php
File in Directory d:\www\test\adodb5\session: adodb-session2.php
File in Directory d:\www\test\adodb5\session: adodb-sessions.mysql.sql
File in Directory d:\www\test\adodb5\session: adodb-sessions.oracle.clob.sql
File in Directory d:\www\test\adodb5\session: adodb-sessions.oracle.sql
File in Directory d:\www\test\adodb5\session: crypt.inc.php
Directory old:
File in Directory d:\www\test\adodb5\session\old: adodb-cryptsession.php
File in Directory d:\www\test\adodb5\session\old: adodb-session-clob.php
File in Directory d:\www\test\adodb5\session\old: adodb-session.php
File in Directory d:\www\test\adodb5\session\old: crypt.inc.php
File in Directory d:\www\test\adodb5\session: session_schema.xml
File in Directory d:\www\test\adodb5\session: session_schema2.xml
Directory tests:
File in Directory d:\www\test\adodb5\tests: benchmark.php
File in Directory d:\www\test\adodb5\tests: client.php
File in Directory d:\www\test\adodb5\tests: pdo.php
File in Directory d:\www\test\adodb5\tests: test-active-record.php
File in Directory d:\www\test\adodb5\tests: test-active-recs2.php
File in Directory d:\www\test\adodb5\tests: test-active-relations.php
File in Directory d:\www\test\adodb5\tests: test-active-relationsx.php
File in Directory d:\www\test\adodb5\tests: test-datadict.php
File in Directory d:\www\test\adodb5\tests: test-perf.php
File in Directory d:\www\test\adodb5\tests: test-pgblob.php
File in Directory d:\www\test\adodb5\tests: test-php5.php
File in Directory d:\www\test\adodb5\tests: test-xmlschema.php
File in Directory d:\www\test\adodb5\tests: test.php
File in Directory d:\www\test\adodb5\tests: test2.php
File in Directory d:\www\test\adodb5\tests: test3.php
File in Directory d:\www\test\adodb5\tests: test4.php
File in Directory d:\www\test\adodb5\tests: test5.php
File in Directory d:\www\test\adodb5\tests: testcache.php
File in Directory d:\www\test\adodb5\tests: testdatabases.inc.php
File in Directory d:\www\test\adodb5\tests: testgenid.php
File in Directory d:\www\test\adodb5\tests: testmssql.php
File in Directory d:\www\test\adodb5\tests: testoci8.php
File in Directory d:\www\test\adodb5\tests: testoci8cursor.php
File in Directory d:\www\test\adodb5\tests: testpaging.php
File in Directory d:\www\test\adodb5\tests: testpear.php
File in Directory d:\www\test\adodb5\tests: testsessions.php
File in Directory d:\www\test\adodb5\tests: test_rs_array.php
File in Directory d:\www\test\adodb5\tests: time.php
File in Directory d:\www\test\adodb5\tests: tmssql.php
File in Directory d:\www\test\adodb5\tests: xmlschema-mssql.xml
File in Directory d:\www\test\adodb5\tests: xmlschema.xml
File in Directory d:\www\test\adodb5: toexport.inc.php
File in Directory d:\www\test\adodb5: tohtml.inc.php
File in Directory d:\www\test\adodb5: xmlschema.dtd
File in Directory d:\www\test\adodb5: xmlschema03.dtd
Directory xsl:
File in Directory d:\www\test\adodb5\xsl: convert-0.1-0.2.xsl
File in Directory d:\www\test\adodb5\xsl: convert-0.1-0.3.xsl
File in Directory d:\www\test\adodb5\xsl: convert-0.2-0.1.xsl
File in Directory d:\www\test\adodb5\xsl: convert-0.2-0.3.xsl
File in Directory d:\www\test\adodb5\xsl: remove-0.2.xsl
File in Directory d:\www\test\adodb5\xsl: remove-0.3.xsl
File in Directory d:\www\test: index.php
File in Directory d:\www\test: index2.php
Directory look:
Directory 123:
File in Directory d:\www\test\look: look.txt
Directory sublook:
File in Directory d:\www\test\look\sublook: index.php
File in Directory d:\www\test: mysqlDataDictionary.php
File in Directory d:\www: test.php
File in Directory d:\www: testExcel.php
File in Directory d:\www: testMath.php
File in Directory d:\www: validateHash.php

你可能感兴趣的:(遍历文件夹)