您当前的位置: 首页 > 网站编程 > PHP教程 > php遍历文件夹和文件列表示例分享

php遍历文件夹和文件列表示例分享

作者:不详 来源:网络 发布时间: 2014-08-06 23:29 点击:
这篇文章主要介绍了php遍历文件夹和文件列表示例,需要的朋友可以参考下 为PHP遍历目录和文件列表写了一个简单的类,并附上使用实例,大家参考使用吧 代码如下: ?php define('DS', DIRECTORY_SEPARATOR); class getDirFile{ //返回数组 private $DirArray = array(); pr

php遍历文件夹和文件列表示例分享

  这篇文章主要介绍了php遍历文件夹和文件列表示例,需要的朋友可以参考下

  为PHP遍历目录和文件列表写了一个简单的类,并附上使用实例,大家参考使用吧

  代码如下:

  <?php

  define('DS', DIRECTORY_SEPARATOR);

  class getDirFile{

  //返回数组

  private $DirArray = array();

  private $FileArray = array();

  private $DirFileArray = array();

  private $Handle,$Dir,$File;

  //获取目录列表

  public function getDir( & $Dir ){

  if( is_dir($Dir) ){

  if( false != ($Handle = opendir($Dir)) ){

  while( false != ($File = readdir($Handle)) ){

  if( $File!='.' && $File!='..' && !strpos($File,'.') ){

  $DirArray[] = $File;

  }

  }

  closedir( $Handle );

  }

  }else{

  $DirArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';

  }

  return $DirArray;

  }

  //获取文件列表

  public function getFile( & $Dir ){

  if( is_dir($Dir) ){

  if( false != ($Handle = opendir($Dir)) ) {

  while( false != ($File = readdir($Handle)) ){

  if( $File!='.' && $File!='..' && strpos($File,'.') ){

  $FileArray[] = $File;

  }

  }

  closedir( $Handle );

  }

  }else{

  $FileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';

  }

  return $FileArray;

  }

  //获取目录/文件列表

  public function getDirFile( & $Dir ){

  if( is_dir($Dir) ){

  $DirFileArray['DirList'] = $this->getDir( $Dir );

  if( $DirFileArray ){

  foreach( $DirFileArray['DirList'] as $Handle ){

  $File = $Dir.DS.$Handle;

  $DirFileArray['FileList'][$Handle] = $this->getFile( $File );

  }

  }

  }else{

  $DirFileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';

  }

  return $DirFileArray;

  }

  }

  ?>

  实例:(相对路径或绝对路径)

  1.获取目录列表

  代码如下:

  <?php

  $Dir_dir = './example';

  $getDirFile = new getDirFile();

  $getDir = $getDirFile->getDir( $Dir_dir );

  print_r($getDir);

  ?>

  显示

  代码如下:

  <?php

  $File_one_dir = './example/example_one';

  $File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';

  $getDirFile = new getDirFile();

  $getFile_one = $getDirFile->getFile( $File_one_dir );

  $getFile_two = $getDirFile->getFile( $File_two_dir );

  print_r($getFile_one);

  print_r($getFile_two);

  ?>

  2.获取文件列表

  代码如下:

  <?php

  $File_one_dir = './example/example_one';

  $File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';

  $getDirFile = new getDirFile();

  $getFile_one = $getDirFile->getFile( $File_one_dir );

  $getFile_two = $getDirFile->getFile( $File_two_dir );

  print_r($getFile_one);

  print_r($getFile_two);

  ?>

  显示

  代码如下:

  Array

  (

  [0] => example.sql

  [1] => example.txt

  )

  Array

  (

  [0] => example.php

  )

  3.获取目录/文件列表

  代码如下:

  <?php

  $Dir_dir = './example';

  $getDirFile = new getDirFile();

  $getDirFile = $getDirFile->getDirFile( $Dir_dir );

  print_r($getDirFile);

  ?>

  显示

  代码如下:

  Array

  (

  [DirList] => Array

  (

  [0] => example_one

  [1] => example_two

  )

  [FileList] => Array

  (

  [example_one] => Array

  (

  [0] => example.sql

  [1] => example.txt

  )

  [example_two] => Array

  (

  [0] => example.php

  )

  )

  )
分享到:
本文"php遍历文件夹和文件列表示例分享"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4