闲着没事,发个PHP遍历目录的代码,供大家参考。
<?php
class listDir {
var $dirPath;
function openDir($dirPath) {
$this->dirPath = $dirPath;
if (is_dir($dirPath)) {
$dir = opendir($dirPath);
return $dir;
}else {
die("$dirPath is Not a Directory");
}
}
function closeDir($dir) {
closedir($dir);
}
function listDir($dir) {
echo '<ol>';
while($file = readdir($dir)) {
if($file!='.' && $file!='..') { // filter . and ..
$dd = $this->dirPath; //
$dd = $dd.'/'.$file;
echo "<li>$file</li>";
}
if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
$subDir = $this->openDir($dd);
$this->listDir($subDir);
$this->closeDir($subDir);
}
}
echo '</ol>';
return true;
}
}$dirOpt = new listDir();
$dirOpt->dirPath = 'C:\AppServ\www\sbp_files';
$dir = $dirOpt->openDir($dirOpt->dirPath);
$dirOpt->listDir($dir);
$dirOpt->closeDir($dir);下面再拷贝一份。
<?php
class listDir {
var $dirPath;
function openDir($dirPath) {
$this->dirPath = $dirPath;
if (is_dir($dirPath)) {
$dir = opendir($dirPath);
return $dir;
}else {
die("$dirPath is Not a Directory");
}
}
function closeDir($dir) {
closedir($dir);
}
function listDir($dir) {
echo '<ol>';
while($file = readdir($dir)) {
if($file!='.' && $file!='..') { // filter . and ..
$dd = $this->dirPath; //
$dd = $dd.'/'.$file;
echo "<li>$file</li>";
}
if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
$subDir = $this->openDir($dd);
$this->listDir($subDir);
$this->closeDir($subDir);
}
}
echo '</ol>';
return true;
}
}$dirOpt = new listDir();
$dirOpt->dirPath = 'C:\AppServ\www\sbp_files';
$dir = $dirOpt->openDir($dirOpt->dirPath);
$dirOpt->listDir($dir);
$dirOpt->closeDir($dir);
<?php
class listDir {
var $dirPath;
function openDir($dirPath) {
$this->dirPath = $dirPath;
if (is_dir($dirPath)) {
$dir = opendir($dirPath);
return $dir;
}else {
die("$dirPath is Not a Directory");
}
}
function closeDir($dir) {
closedir($dir);
}
function listDir($dir) {
echo '<ol>';
while($file = readdir($dir)) {
if($file!='.' && $file!='..') { // filter . and ..
$dd = $this->dirPath; //
$dd = $dd.'/'.$file;
echo "<li>$file</li>";
}
if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
$subDir = $this->openDir($dd);
$this->listDir($subDir);
$this->closeDir($subDir);
}
}
echo '</ol>';
return true;
}
}$dirOpt = new listDir();
$dirOpt->dirPath = 'C:\AppServ\www\sbp_files';
$dir = $dirOpt->openDir($dirOpt->dirPath);
$dirOpt->listDir($dir);
$dirOpt->closeDir($dir);下面再拷贝一份。
<?php
class listDir {
var $dirPath;
function openDir($dirPath) {
$this->dirPath = $dirPath;
if (is_dir($dirPath)) {
$dir = opendir($dirPath);
return $dir;
}else {
die("$dirPath is Not a Directory");
}
}
function closeDir($dir) {
closedir($dir);
}
function listDir($dir) {
echo '<ol>';
while($file = readdir($dir)) {
if($file!='.' && $file!='..') { // filter . and ..
$dd = $this->dirPath; //
$dd = $dd.'/'.$file;
echo "<li>$file</li>";
}
if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
$subDir = $this->openDir($dd);
$this->listDir($subDir);
$this->closeDir($subDir);
}
}
echo '</ol>';
return true;
}
}$dirOpt = new listDir();
$dirOpt->dirPath = 'C:\AppServ\www\sbp_files';
$dir = $dirOpt->openDir($dirOpt->dirPath);
$dirOpt->listDir($dir);
$dirOpt->closeDir($dir);
$dataDir = dirname(__FILE__).'/world_vest_base/';
try
{
$dir = new DirectoryIterator($dataDir); foreach ($dir as $file)
{
$fileName = $file->getFilename();
}
}
catch (Exception $ex)
{ } ?> queryphp 简单 MVC ORM框架
---------------------------------------------
http://topic.csdn.net/u/20100310/11/62a60067-b3da-4dee-9d6a-f1baeb2f3f33.html
class listDirClass {
var $dirPath;
function openDir($dirPath) {
$this->dirPath = $dirPath;
if (is_dir($dirPath)) {
$dir = opendir($dirPath);
return $dir;
}else {
die("$dirPath is Not a Directory");
}
}
function closeDir($dir) {
closedir($dir);
}
function listDir($dir) {
echo '<ol>';
while($file = @readdir($dir)) {
if($file!='.' && $file!='..') { // filter . and ..
$dd = $this->dirPath; //
$dd = $dd.'/'.$file;
echo "<li>$file</li>";
}
if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 参数需要完整的路径
$subDir = $this->openDir($dd);
$this->listDir($subDir);
$this->closeDir($subDir);
}
}
echo '</ol>';
return true;
}
}$dirOpt = new listDirClass();
$dirOpt->dirPath = 'D:\minzheng\DBUnity\obj';
$dir = $dirOpt->openDir($dirOpt->dirPath);
$dirOpt->listDir($dir);
$dirOpt->closeDir($dir);?>
我也这么觉得,所以我不会写class ...为什么那么多人都喜欢写class ?不太习惯