Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php 遍历指定路径下所有目录与文件(示例)

php 遍历指定路径下所有目录与文件(示例)

WBOY
WBOYasal
2016-07-25 08:55:101186semak imbas
  1. /**
  2. * php 遍历目录与文件
  3. * by bbs.it-home.org
  4. */
  5. function scan_all($dir){
  6. $temp = scandir($dir);
  7. if(is_array($temp) && count($temp)>2){
  8. array_shift($temp);
  9. array_shift($temp);
  10. foreach($temp as $v){
  11. $cur_dir=$dir.DIRECTORY_SEPARATOR.$v;
  12. if(is_dir($cur_dir)){
  13. echo $v." =>";
  14. echo "
    ";
  15. scan_all($cur_dir);
  16. echo "
  17. ";
  18. }else if(is_file($cur_dir)){
  19. echo "FILE:".$v."
    ";
  20. }else{
  21. echo 'err';
  22. }
  23. }
  24. }
  25. }
  26. $dir = "D:/yourdir/";
  27. scan_all($dir);
复制代码

方法2,DirectoryIterator:

  1. /**

  2. * php 遍历目录与文件
  3. * by bbs.it-home.org
  4. */
  5. function directoryiterator($path)
  6. {
  7. $iterator = new DirectoryIterator($path);
  8. foreach ($iterator as $fileinfo) {

  9. if(!$fileinfo->isDot())
  10. {
  11. if($fileinfo->isDir())
  12. {
  13. echo $fileinfo.'=>
    ';
  14. directoryiterator($path.DIRECTORY_SEPARATOR.$fileinfo);
  15. echo '
  16. ';
  17. }
  18. else
  19. {
  20. echo $fileinfo->getFilename().'
    ';
  21. }
  22. }
  23. }
  24. }
  25. $dir = "D:/yourdir/";
  26. directoryiterator($dir);
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn