フォルダ内のファイル一覧

function scanDirectory($node, $limit=0, $separator=""){
	//validate:
	if(!is_dir($node)){return null;}; $newSeparator=($separator=="")?"/":$separator;
	$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
	$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
	substr($node, 0, strlen($node)-1):$node;
	//initialize:
	$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
	$stack=array( array($node, $slashes) ); $directories=array(); $files=array(); $output=array();
	//RUN:
		while(sizeof($stack) && $stack[0][1]!=$limit){
		$current=array_shift($stack);
		$items=opendir($current[0]);
			while( ($it=readdir($items))!==false ){
				if( strrpos($it,".") === (strlen($it)-1) ){continue;};
				$path=$current[0].$newSeparator.$it;
				$output[]=$path;
				if(is_dir($path)){
					$files = dirList($path);
					$directories[$path]=$files; 
					$stack[]=array( $path, substr_count($path, $newSeparator) );
				};
			};
		}

	return $directories;
	/* keep this comment to reuse freely:*/
}

function dirList ($directory) 
{

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = mb_convert_encoding($file, "EUC-JP", "SJIS, UTF-8, JIS, ASCII");
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

$a = scanDirectory($path);

source

Comments are closed.