opendir() 函数
opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用。
若成功,则该函数返回一个目录流,否则返回 false 以及一个 error。可以通过在函数名前加上 "@" 来隐藏 error 的输出。
语法为 opendir(path,context)。
- 参数 path,必需。规定要打开的目录路径。
- 参数 context,可选。规定目录句柄的环境。context 是可修改目录流的行为的一套选项。
下面是一个例子:
<?php //打开 images 目录 $dir = opendir("bkjia"); //列出 images 目录中的文件 while (($file = readdir($dir)) !== false) { echo "filename: " . $file . "<br />"; } closedir($dir); ?>
程序输出:
filename: . filename: .. filename: cat.gif filename: dog.gif filename: food filename: horse.gif
这里把子目录和文件都输出了,现在只需要把子目录输出,可以用下面的函数实现:
<?php function getSubDirs($dir) { $subdirs = array(); if(!$dh = opendir($dir)) return $subdirs; $i = 0; while ($f = readdir($dh)) { if($f =='.' || $f =='..') continue; //如果只要子目录名, path = $f; //$path = $dir.'/'.$f; $path = $f; $subdirs[$i] = $path; $i++; } return $subdirs; } $arr = getSubDirs("tmp"); print_r($arr); ?>
程序运行结果为:
Array ( [0] => Hello [1] => NowaMagic )
这次可以实现需求了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
