php 返回目录下的所有文件名/文件夹类
浏览:3333次 出处信息
这里为了便于演示,直接列出来了,其中可以返回数组之类的,根据需要另作改动。
代码如下,可以直接Copy测试。
- <?php
- main();
- function main(){
- //Set Variables
- $testDirName = "./zhoz/";
- $fileListArray = array();
- $dirListArray = array();
- $aDirectory = new DirReader($testDirName);
- // Store File List in Array
- $fileListArray = $aDirectory->getFileList();
- $dirListArray = $aDirectory->getDirList();
- echo "<html><body>\n";
- echo "<pre>\n";
- echo "Reading Directory: ". $aDirectory->getDirPath() ."\n";
- echo "Current Directory: ". getcwd() ."\n";
- echo "-- Files in Directory --\n";
- foreach ($fileListArray as $filename) {
- echo "File: $filename\n";
- }
- echo "\n-- Sub Directories --\n";
- foreach ($dirListArray as $filename){
- echo "Sub dir: $filename\n";
- }
- echo "</pre></body></html>\n";
- }
- class DirReader{
- private $dh;
- private $basedir;
- private $fileNameArray=array();
- private $dirNameArray=array();
- function __construct($dirname){
- $this->basedir = $dirname;
- $this->dh = dir($dirname) or die($php_errormsg);
- $this->parseDirectory();
- }
- function parseDirectory(){
- $filename = "";
- while (false !== ($filename = $this->dh->read())){
- $fullpath = $this->basedir . '/' . $filename;
- if (is_file($fullpath)){
- array_push($this->fileNameArray, $filename);
- }else{
- array_push($this->dirNameArray, $filename);
- }
- }
- }
- // Get all the files in the directory
- function getFileList(){
- return $this->fileNameArray;
- }
- // Get all the sub directories in the directory
- function getDirList(){
- return $this->dirNameArray;
- }
- function getDirPath(){
- return $this->dh->path;
- }
- }
建议继续学习:
- Bash 小技巧:给目录加上书签,快速切换目录 (阅读:6708)
- 查找当前目录的重复文件 (阅读:3073)
- bash遍历目录 (阅读:2827)
- linux文件目录操作总结 (阅读:2735)
- apache配置(如何禁止列出目录内容) (阅读:2583)
- MySql重启命令与数据库安装目录 (阅读:2490)
- FREEBSD 建目录上限 (阅读:2376)
- Linux(CentOS)下更改/转移MySQL数据库目录 (阅读:2251)
- 记一个php函数dirname (阅读:2192)
- 递归创建目录的一个函数 (阅读:2140)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:用PHP计算身份证校验码
后一篇:使用bcompiler对PHP文件进行加密 >>
文章信息
- 作者:zhoz <zhozweb@yahoo.com.cn> 来源: Everyday NetLog
- 标签: 目录
- 发布时间:2010-03-24 23:37:49
建议继续学习
近3天十大热文
-
[63] memory prefetch浅析
-
[52] 转载:cassandra读写性能原理分析
-
[49] 深入浅出cassandra 4 数据一致性问
-
[40] 获取Dom元素的X/Y坐标
-
[39] 《web前端最佳实践》—高维护性css
-
[39] MySQL半同步存在的问题
-
[39] 字符引用和空白字符
-
[38] javascript插入样式
-
[38] 基本排序算法的PHP实现
-
[37] JS中如何判断字符串类型的数字