php列出一个目录下的所有文件

利用php的glob函数可以列出所有切合路径布局的文件和目次,以下从某cms提取出来的函数

<?php function dir_path($path) { $path = str_replace('\\', '/', $path); if (substr($path, -1) != '/') $path = $path . '/'; return $path; } /** * 列出目次下的所有文件 * * @param str $path 目次 * @param str $exts 后缀 * @param array $list 路径数组 * @return array 返回路径数组 */ function dir_list($path, $exts = '', $list = array()) { $path = dir_path($path); $files = glob($path . '*'); foreach($files as $v) { if (!$exts || preg_match("/\.($exts)/i", $v)) { $list[] = $v; if (is_dir($v)) { $list = dir_list($v, $exts, $list); } } } return $list; } ?>

利用要领:

<?php $r = dir_list('dir'); printf("<p>输出数据为:</p><pre>%s</pre>\n", var_export($r , true)); ?>

end...

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7807.html