Posted By

sekihin on 01/28/10


Tagged

file


Versions (?)

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


 / Published in: PHP
 

URL: http://www.miklabo.com/boxes/web/php/dir.html

http://www.fullposter.com/snippets.php?snippet=13 http://sourceforge.jp/projects/sfnet_dir-list/

  1. function scanDirectory($node, $limit=0, $separator="\\"){
  2. //validate:
  3. if(!is_dir($node)){return null;}; $newSeparator=($separator=="\\")?"/":$separator;
  4. $node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
  5. $node=(strrpos($node, $newSeparator)==strlen($node)-1)?
  6. substr($node, 0, strlen($node)-1):$node;
  7. //initialize:
  8. $slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
  9. $stack=array( array($node, $slashes) ); $directories=array(); $files=array(); $output=array();
  10. //RUN:
  11. while(sizeof($stack) && $stack[0][1]!=$limit){
  12. $current=array_shift($stack);
  13. $items=opendir($current[0]);
  14. while( ($it=readdir($items))!==false ){
  15. if( strrpos($it,".") === (strlen($it)-1) ){continue;};
  16. $path=$current[0].$newSeparator.$it;
  17. $output[]=$path;
  18. if(is_dir($path)){
  19. $files = dirList($path);
  20. $directories[$path]=$files;
  21. $stack[]=array( $path, substr_count($path, $newSeparator) );
  22. };
  23. };
  24. }
  25.  
  26. return $directories;
  27. /* keep this comment to reuse freely:*/
  28. }
  29.  
  30. function dirList ($directory)
  31. {
  32.  
  33. // create an array to hold directory list
  34. $results = array();
  35.  
  36. // create a handler for the directory
  37. $handler = opendir($directory);
  38.  
  39. // keep going until all files in directory have been read
  40. while ($file = readdir($handler)) {
  41.  
  42. // if $file isn't this directory or its parent,
  43. // add it to the results array
  44. if ($file != '.' && $file != '..')
  45. $results[] = mb_convert_encoding($file, "EUC-JP", "SJIS, UTF-8, JIS, ASCII");
  46. }
  47.  
  48. // tidy up: close the handler
  49. closedir($handler);
  50.  
  51. // done!
  52. return $results;
  53.  
  54. }
  55.  
  56. $a = scanDirectory($path);

Report this snippet  

You need to login to post a comment.