php 遍历目录文件方法
来源:互联网 发布:linux 发送文件 编辑:程序博客网 时间:2024/05/29 10:58
<?php/**********************一个简单的目录递归函数第一种实现办法:用dir返回对象***********************/function tree($directory) { if(is_dir($directory)) { //返回一个 Directory 类实例 $mydir = dir($directory); echo "<ul>\n"; //从目录句柄中读取条目 while($file = $mydir->read()) { if(is_dir("$directory/$file") && $file != "." && $file != "..") { echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n"; //递归读取目录 tree("$directory/$file"); } elseif ($file != "." && $file != "..") { echo "<li>$file</li>\n"; } } echo "</ul>\n"; // 释放目录句柄 $mydir->close(); } else { echo $directory . '<br>'; }} //开始运行echo "<h2>目录为粉红色</h2><br>\n"; tree("../www.test.com");
<?php /***********************第二种实现办法:用readdir()函数************************/function listDir($dir) { if(is_dir($dir)) { //打开目录句柄 if ($dh = opendir($dir)) { //从目录句柄中读取条目 while (($file = readdir($dh)) !== false) { if(is_dir($dir."/".$file) && $file!="." && $file!="..") { echo "<b><font color='red'>文件名:</font></b>",$file,"<br><hr>"; listDir($dir."/".$file."/"); } else { if($file != "." && $file != "..") { echo $file."<br>"; } } } closedir($dh); } } else { echo $dir . '<br>'; }}//开始运行listDir("../www.test.com");?>
以数组形式返回整个文件夹中多重文件
<?php/** * Create a Directory Map * * Reads the specified directory and builds an array * representation of it. Sub-folders contained with the * directory will be mapped as well. * * @param string $source_dir Path to source * @param int $directory_depth Depth of directories to traverse * (0 = fully recursive, 1 = current dir, etc) * @param bool $hidden Whether to show hidden files * @return array */function directory_map($source_dir, $directory_depth = 0, $hidden = FALSE) { if ($fp = @opendir($source_dir)) { $filedata = array(); $new_depth= $directory_depth - 1; $source_dir= rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; while (FALSE !== ($file = readdir($fp))) { // Remove '.', '..', and hidden files [optional] if ($file === '.' OR $file === '..' OR ($hidden === FALSE && $file[0] === '.')) { continue; } is_dir($source_dir.$file) && $file .= DIRECTORY_SEPARATOR; if (($directory_depth < 1 OR $new_depth > 0) && is_dir($source_dir.$file)) { $filedata[$file] = directory_map($source_dir.$file, $new_depth, $hidden); } else { $filedata[] = $file; } } closedir($fp); return $filedata; } return FALSE; } $aa = directory_map('../www.test.com'); var_dump($aa);
0 0
- php 遍历目录文件方法
- php遍历文件目录
- PHP 遍历目录方法
- PHP 遍历目录下的所有文件的方法
- PHP 遍历目录下的所有文件的方法
- PHP遍历目录文件的几种方法效率评析
- PHP: 遍历目录下特定文件的若干方法
- php遍历目录及文件
- PHP目录文件遍历实现
- PHP遍历文件目录笔记
- PHP实现目录文件遍历
- php递归遍历文件目录
- java遍历文件目录方法
- PHP递归遍历指定目录的文件并统计文件数量的方法
- 使用PHP遍历目录中的文件
- PHP遍历目录下全部文件
- php 递归遍历目录与文件
- php遍历目录的所用文件
- 2008-11-10 12:48 为centos添加EPEL软件仓库
- apache的dbUtils的使用
- vnc
- linux查看CPU信息、机器型号等硬件信息
- 【KVM2】xauth:(stdin):1:bad display name \"xxx:1\" in \"add\" command解决
- php 遍历目录文件方法
- 记小问题:linux下mysql的表名默认区分大小写
- Android studio 中gson混淆,及注意的地方
- html5解决大文件断点续传
- Linux more 命令用法详解
- db更改密码
- mstsc port
- ssh port
- Butter Knife 7.0.1 使用方法