PHP 显示指定文件夹内的文件

发布时间:2008年12月17日      浏览次数:580 次
<?php
error_reporting(0);
header("content-type:text/html; charset=UTF-8");
#######################################################
# 函数 view_folder() ,将指定文件夹内的所有文件赋予数组并返回,不包括内部子目录中的文件
# 参数说明:
# $folder_name 指定要遍历的目录路径
# $start_str 只查询文件名以该字符串开始的文件
# $not_str 指定要排除的文件,多个以 || 分隔开
#######################################################
function view_folder($folder_name = ".", $start_str = "", $not_str = ""){
      if ( trim($folder_name) == "" ) return $file_arr;
      $start_str_len = strlen( trim($start_str) );
      $not_str_arr = explode( "||", trim($not_str) );
      $handle=opendir($folder_name);
      while ($file = readdir($handle)) {
            if( !is_dir($file)) {
                  if ( $start_str_len > 0 ){
                        if ( strtolower(substr($file,0,$start_str_len)) == strtolower(trim($start_str)) ){
                              $no_str_yes = "no";
                              if ( count($not_str_arr) > 0 ){
                                    foreach($not_str_arr as $key => $value) {
                                          if ( strtolower(trim($not_str_arr[$key])) == strtolower($file) ){
                                                $no_str_yes = "yes";
                                                break;
                                          }
                                    }
                              }
                              if ( $no_str_yes == "no" ){
                                    $file_arr[] = "$file";
                              }
                        }
                  }else{
                        $no_str_yes = "no";
                        foreach($not_str_arr as $key => $value) {
                              if ( trim($not_str_arr[$key]) == $file ){
                                    $no_str_yes = "yes";
                                    break;
                              }
                        }
                        if ( $no_str_yes == "no" ){
                              $file_arr[] = "$file";
                        }            
                  }
            }
      }
      closedir($handle);
      if ( count($file_arr) > 0 ) sort($file_arr); //排序数组
      return $file_arr;
}
#######################################################
# 调用函数示例
#######################################################
$folder_name = ".";
$start_str = trim($_GET["str"]);
$not_str = ".||..";
$file_arr = view_folder($folder_name, $start_str, $not_str);
if ( count($file_arr) > 0 ){
      foreach($file_arr as $key => $value){
            echo "<div><a href='".$file_arr[$key]."'>".$file_arr[$key]."</a></div>";
      }
}
?>
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!