php 返回目录下的所有文件名/文件夹类
浏览:3221次 出处信息
这里为了便于演示,直接列出来了,其中可以返回数组之类的,根据需要另作改动。
代码如下,可以直接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 小技巧:给目录加上书签,快速切换目录 (阅读:6284)
- 查找当前目录的重复文件 (阅读:2958)
- bash遍历目录 (阅读:2628)
- linux文件目录操作总结 (阅读:2619)
- MySql重启命令与数据库安装目录 (阅读:2405)
- apache配置(如何禁止列出目录内容) (阅读:2366)
- FREEBSD 建目录上限 (阅读:2290)
- Linux(CentOS)下更改/转移MySQL数据库目录 (阅读:2156)
- 记一个php函数dirname (阅读:2124)
- 递归创建目录的一个函数 (阅读:2047)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:用PHP计算身份证校验码
后一篇:使用bcompiler对PHP文件进行加密 >>
文章信息
- 作者:zhoz <zhozweb@yahoo.com.cn> 来源: Everyday NetLog
- 标签: 目录
- 发布时间:2010-03-24 23:37:49
建议继续学习
近3天十大热文
- [12] 产品设计之QQ邮箱登录页与淘宝登录页
- [9] 分布式系统的事务处理
- [9] Python连接 MySQL 数据库的超时问
- [8] 移动Web开发初学者指南
- [8] 读书:谣言
- [8] 浅述各平台系统交互设计的不同关注点
- [8] 关于不得不在python中使用代理访问网络的
- [8] 迈过社会化网络:互联网的新时代
- [8] 一个 VLA (可变长度数组)的实现
- [7] iptables 看门狗