php - how to turn a recursive function opendir -
would transform recursive function below. keeping filter of allowed file extension (xml).
function lista_xml($path) { $xml_array = array(); $dh = opendir($path); while ( false !== ($file = readdir($dh)) ) { if ( $file=="." || $file==".." || is_dir($file) ) continue; $namearr = explode('.',$file); if ($namearr[count($namearr)-1] == 'xml') $xml_array[] = $file; } closedir($dh); return $xml_array; } folder: path/directory1/aaa.xml;bbb.xml; path/directory1/directory2/xxx.xml;yyy.xml; path/directory1/directory2/directory3/ccc.xml; want unique array: [0] => aaa.xml [1] => bbb.xml [2] => xxx.xml [3] => yyy.xml [4] => ccc.xml
make return variable input variable , recursively call on subdirectories.
function lista_xml($path, $xml_array=array()) { $dh = opendir($path); while ( false !== ($file = readdir($dh)) ) { if ($file == '.' || $file == '..') continue; $file = realpath($path . '/' .basename($file)); $info = pathinfo($file); if (isset($info['extension']) && $info['extension'] == 'xml') { $xml_array[] = $file; } elseif ( $file!='.' && $file!='..' && is_dir($file)) { $xml_array = lista_xml($file, $xml_array); } } closedir($dh); return $xml_array; }
Comments
Post a Comment