function dir_walk($path = '', $depth = 0) { if ($path == '') { $apath = './'; } else { if (substr($path, -1, 1) != '/') { $path .= '/'; } $apath = $path; } $out = array(); $ds = utf8_encode(str_repeat("� � ", $depth)); foreach (scandir($apath) as $v) { if (substr($v, 0, 1) == '.') { continue; } if (is_dir($apath . $v)) { printf("%s+ <b>%s</b><br/>\n", $ds, xml_escape($path . $v)); dir_walk($path . $v, $depth + 1); print "<br/>\n"; } else { if (ext($v) == 'pyc' || $path == '' && $v == 'index.php') { continue; } $out[] = $v; } } foreach ($out as $file) { printf("%s<a href=\"?sauce=%s\">%s</a><br/>\n", $ds, urlencode($path . $file), xml_escape($file)); } }
/** * Calls a function for every file in a folder. * * @author Vasil Rangelov a.k.a. boen_robot * * @param string $callback The function to call. It must accept one argument that is a relative filepath of the file. * @param string $dir The directory to traverse. * @param array $types The file types to call the function for. Leave as NULL to match all types. * @param bool $recursive Whether to list subfolders as well. * @param string $baseDir String to append at the beginning of every filepath that the callback will receive. */ function dir_walk($callback, $dir, $types = null, $ignore = null, $recursive = false, $baseDir = '') { $dir = rtrim($dir, '/'); debug_output("opening directory " . $dir, 0); if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file === '.' || $file === '..') { continue; } $continue = true; if (isset($GLOBALS['ignore'])) { foreach ($GLOBALS['ignore'] as $regex) { $test = preg_match($regex, $file); if ($test && count($test)) { debug_output('found "' . $regex . '" skipping file ' . $file, 1); $continue = false; } elseif (preg_last_error()) { debug_output('Bad regex "' . $regex . '" preg_last_error = ' . preg_error_output(preg_last_error()), 5); } } } if ($continue) { $file_path = $dir . '/' . $file; if (is_file($file_path)) { debug_output('found file ' . $file_path, 0); if (is_array($types)) { $pathinfo = pathinfo($file_path, PATHINFO_EXTENSION); debug_output($pathinfo, 0); if (!in_array(strtolower($pathinfo), $types, true)) { continue; } } $callback($baseDir . $file); } elseif ($recursive && is_dir($file_path)) { dir_walk($callback, $file_path . DIRECTORY_SEPARATOR, $types, $ignore, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR); } else { debug_output('doing nothing ' . $file_path); } } } closedir($dh); } else { echo "unable to open directory {$dir}\r\n"; } debug_output("leaving directory " . $file, 0); }