PHP List Directory structure and exclude some directories -
i have php code:
$rootpath = '../admin/'; $inner = new recursivedirectoryiterator($rootpath); $fileinfos = new recursiveiteratoriterator($inner); foreach ($fileinfos $pathname => $fileinfo) { $pathname2 = substr($pathname,2); $sql = "select * admin_permissions page_name = '$pathname2'"; $rs = mysql_query($sql,$conn); if (mysql_num_rows($rs) == 0) { if (!$fileinfo->isfile()) continue; $sql2 = "insert admin_permissions (page_name) values ('$pathname2')"; $rs2 = mysql_query($sql2,$conn); echo "$pathname<br>"; } } that displaying directory structure , inserting directories , file names database (removing first 2 characters ..).
since recursivedirectoryiterator iterates through files in directories, how can exclude whole directories, including files within them?
in essence, answer not different thomas' answer. however, not few things correct:
- the semantics correct
recursivecallbackfilteriteratorrequire returntruerecurse subdirectories. - he doesn't skip
.,..directories inside each sub-directory - his
in_arraycheck doesn't quite expects
so, wrote answer instead. work correctly, assuming understand want:
edit: has since fixed 2 of 3 issues; third may not issue because of way wrote conditional check not quite sure.
<?php $directory = '../admin'; // exclude under these directories $exclude = array('.git', 'otherdirtoexclude'); /** * @param splfileinfo $file * @param mixed $key * @param recursivecallbackfilteriterator $iterator * @return bool true if need recurse or if item acceptable */ $filter = function ($file, $key, $iterator) use ($exclude) { if ($iterator->haschildren() && !in_array($file->getfilename(), $exclude)) { return true; } return $file->isfile(); }; $inneriterator = new recursivedirectoryiterator( $directory, recursivedirectoryiterator::skip_dots ); $iterator = new recursiveiteratoriterator( new recursivecallbackfilteriterator($inneriterator, $filter) ); foreach ($iterator $pathname => $fileinfo) { // insertion here }
Comments
Post a Comment