Heim  >  Artikel  >  Backend-Entwicklung  >  php遍历目录二例代码

php遍历目录二例代码

WBOY
WBOYOriginal
2016-07-25 08:53:49994Durchsuche
  1. /*
  2. * 遍历目录
  3. * fopen()
  4. * fread()
  5. * fclose()
  6. * @opendir(目标路径);E_WARNINE
  7. * readdir(目录引用句柄) rewinddir()
  8. * closedir()
  9. *
  10. * 在程序设计中,句柄是一种特殊的智能指针 。当一个应用程序要引用其他系统(如数据库、操作系统)
  11. * 所管理的内存块或对象时,就要使用句柄。句柄与普通指针的区别在于,
  12. * 指针包含的是引用对象的内存地址,而句柄则是由系统所管理的引用标识,
  13. * 该标识可以被系统重新定位到一个内存地址上。这种间接访问对象的模式增强了系统对引用对象的控制。
  14. * (脚本学堂 bbs.it-home.org)
  15. */
  16. $dirHandle=@opendir("phpMyAdmin") or die("打开目录不成功");
  17. echo "phpMyAdmin 目录下所有内容是:
    ";
  18. echo readdir($dirHandle)."
    ";
  19. echo readdir($dirHandle)."
    ";
  20. while(($file=readdir($dirHandle))!==false)
  21. {
  22. $file="phpMyadmin".DIRECTORY_SEPARATOR.$file;
  23. if(is_dir($file))
  24. {
  25. echo "目录: ".$file."
    ";
  26. }else
  27. {
  28. echo "文件: ".$file."文件大小: ".filesize($file)."KB
    ";
  29. }
  30. }
  31. rewinddir($dirHandle); //返回句柄开始,重新遍历一次
  32. while(($file=readdir($dirHandle))!==false)
  33. {
  34. $file="phpMyadmin".DIRECTORY_SEPARATOR.$file;
  35. if($file!="."&&$file!="..") //直接不读
  36. {
  37. if(is_dir($file))
  38. {
  39. echo "目录: ".$file."
    ";
  40. }else
  41. {
  42. echo "文件: ".$file."文件大小: ".filesize($file)."KB
    ";
  43. }
  44. }
  45. }
  46. closedir($dirHandle);
  47. ?>
复制代码

例2,

  1. /*
  2. * 遍历目录
  3. *
  4. * class dir{
  5. * string path;
  6. * resource handle;
  7. * string read(void);
  8. * void rewind(void);
  9. * void close(void);
  10. * }
  11. *
  12. * 在程序设计中,句柄是一种特殊的智能指针 。当一个应用程序要引用其他系统(如数据库、操作系统)
  13. * 所管理的内存块或对象时,就要使用句柄。句柄与普通指针的区别在于,
  14. * 指针包含的是引用对象的内存地址,而句柄则是由系统所管理的引用标识,
  15. * 该标识可以被系统重新定位到一个内存地址上。这种间接访问对象的模式增强了系统对引用对象的控制。
  16. *
  17. */
  18. $d = dir("phpMyAdmin");
  19. echo "路径是:".$d->path."
    ";
  20. echo "引用句柄是: ". $d->handle."
    ";
  21. // $d->read();
  22. // $d->read();
  23. while(($file=$d->read())!==false)
  24. {
  25. if($file!="."&&$file!="..")
  26. {
  27. echo $file."
    ";
  28. }
  29. }
  30. $d->close();
  31. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn