Código :
<?php //$rDir=2 listara los directorios //$rDir=1 listara pero no mostrara su contenido //$rDir=0 no listara ni mostrara su contenido function read_dir($dir, $rDir = 0){ if (!is_dir($dir)){ return false; } $cDir = dir ($dir); //cDir = current dir; $dirname = " ".dir_name($dir); $aDir[$dirname] = array(); while (false !== ($file = $cDir->read())){ if (($file != ".") && ($file !="..")){ $isDir = is_dir($dir."/".$file); if (($isDir) && ($rDir==2)){ $aDir[$dirname] = array_merge($aDir[$dirname],read_dir($dir."/".$file, $rDir)); } else if ( ($isDir) && ($rDir>0)){ $aDir[$dirname][$file] = array(); } else if (!$isDir){ $aDir[$dirname][] = $file; } } } $cDir->close(); return $aDir; } function dir_name ($dir){ $dir = realpath($dir); $pos = strrpos($dir,"\\"); //Windows if ($pos === false){ $pos = strrpos($dir,"/"); //Linux :) } $dir = substr($dir,$pos+1,strlen($dir)-$pos); return $dir; } echo "<pre>"; print_r(read_dir("sofwtare/projects/",2)); echo "</pre>"; ?>
salida:
Código :
Array ( [ projects] => Array ( [ classes] => Array ( [0] => FileManager.php [1] => Prueba.as [2] => User.php [3] => Resumen.doc ) [ CVS] => Array ( [0] => archivo.txt [1] => nas.zip [2] => Untitled-2.swf ) ) )