zoukankan      html  css  js  c++  java
  • php遍历文件夹下的所有文件及文件夹

    //第一种 遍历放入数据中
    function my_scandir($dir)
    {
    	$files = array();
    	if ( $handle = opendir($dir) ) {
    		while ( ($file = readdir($handle)) !== false ) 
    		{
    			if ( $file != ".." && $file != "." ) 
    			{
    				if ( is_dir($dir . "/" . $file) ) 
    				{
    					$files[$file] = my_scandir($dir . "/" . $file);
    				}
    				else 
    				{
    					$files[] = $file;
    				}
    			}
    		}
    		closedir($handle);
    		return $files;
    	}
    }
    //第二种 直接输出
    function traverse($path = '.') {
    	$current_dir = opendir ( $path ); //opendir()返回一个目录句柄,失败返回false
    	while ( ($file = readdir ( $current_dir )) !== false ) { //readdir()返回打开目录句柄中的一个条目
    		$sub_dir = $path . DIRECTORY_SEPARATOR . $file; //构建子目录路径
    		if ($file == '.' || $file == '..') {
    			continue;
    		} else if (is_dir ( $sub_dir )) { //如果是目录,进行递归
    			echo '文件目录 ' . $file . ':<br>';
    			traverse ( $sub_dir );
    		} else { //如果是文件,直接输出
    			echo '文件路径' . $path . ': ' . $file . '<br>';
    		}
    	}
    }
    

      

  • 相关阅读:
    vue-实践1
    node 基本使用
    vue通信
    初始AMD和Common.js
    vue正确引入第三方包
    常见的java设计模式
    springboot加ES实现全局检索
    Cookie丢失的原因
    动态SQL
    用Java实现给图片添加文字
  • 原文地址:https://www.cnblogs.com/lixiuran/p/3614816.html
Copyright © 2011-2022 走看看