<?php
error_reporting(0);
header("content-type:text/html; charset=UTF-8");
#######################################################
# 函数 view_folder() ,将指定文件夹内的所有文件赋予数组并返回
# 参数说明:
# $folder_arr 返回值变量,子目录数组
# $file_arr 返回值变量,当前目录的文件数组
# $path 指定要遍历的目录路径
# $start_str 只查询文件名以该字符串开始的文件
# $not_str 指定要排除的文件,多个以 || 分隔开
#######################################################
function view_folder(&$folder_arr, &$file_arr, $path = ".", $start_str = "", $not_str = ""){
if ( trim($path) == "" ) return $file_arr;
$start_str_len = strlen( trim($start_str) );
$not_str = ".||..||".$not_str;
$not_str_arr = explode( "||", trim($not_str) );
$handle=opendir($path);
while ($file = readdir($handle)) {
$path = realpath(str_replace('\\','/',$path));
if( !is_dir( $path."/".$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";
}
}
}else{
if ( $file <> "." ) $folder_arr[] = $file;
}
}
closedir($handle);
if ( count($folder_arr) > 0 ) sort($folder_arr); //排序数组
if ( count($file_arr) > 0 ) sort($file_arr); //排序数组
}
#######################################################
# 调用函数示例
#######################################################
//设置参数,并调用函数
$path = trim($_GET['path']); //获取当前目录路径
if( $path == "" ) $path = dirname(__FILE__);
$path = str_replace('\\','/',$path);
$start_str = ""; //只显示首字母等于此字符串的文件(此参数功能是用于特殊用途的,不需要请留空)
$not_str = ""; //要排除显示的文件名,多个以 || 分隔开(此参数功能是用于特殊用途的,不需要请留空)
view_folder($folder_arr, $file_arr, $path, $start_str, $not_str); //调用函数返回数组
$path_url = realpath($path); //将路径转换成实际有效的路径
echo "<b>当前目录:".$path_url."</b><br>";
//输出目录
if ( count($folder_arr) > 0 ){
foreach($folder_arr as $key => $value){
if ( trim($folder_arr[$key]) == ".." ){
echo "<div>[<a href='?path=".urlencode($path_url)."/".$folder_arr[$key]."'><font color=#000>".$folder_arr[$key]."</font></a>]</div>";
}else{
echo "<div>[<a href='?path=".urlencode($path_url)."/".urlencode($folder_arr[$key])."'><font color=#000>".$folder_arr[$key]."</font></a>]</div>";
}
}
}
//输出当前目录的文件
if ( count($file_arr) > 0 ){
$root_path = substr($_SERVER['DOCUMENT_ROOT'],0,strlen($_SERVER['DOCUMENT_ROOT'])-1); //根目录
$root_path_len = strlen($root_path);
$this_path = str_replace('\\','/',$path_url); //当前目录
$this_path_len = strlen($this_path);
if ( $root_path_len <= $this_path_len and substr($root_path,0,$root_path_len)==substr($this_path,0,$root_path_len) ){ //当在根目录范围内时,文件则以链接方式显示
$this_url = str_replace($root_path,"",$this_path); //去掉根目录绝对路径,使其以/开始
if ( $this_url == "" ){ //如果完全相等,则默认从根目录/开始
$this_url = "/";
}else{ //否则在目录后面加上“/”结尾,以连接文件名组成URL
$this_url .= "/";
}
//循环输出文件
foreach($file_arr as $key => $value){
echo "<div><a href='".$this_url.$file_arr[$key]."'target='_blank'>".$file_arr[$key]."</a></div>";
}
}else{ //当已超出根目录时,只显示文件,取消超链接
foreach($file_arr as $key => $value){
echo "<div>".$file_arr[$key]."</div>";
}
}
}
?>
error_reporting(0);
header("content-type:text/html; charset=UTF-8");
#######################################################
# 函数 view_folder() ,将指定文件夹内的所有文件赋予数组并返回
# 参数说明:
# $folder_arr 返回值变量,子目录数组
# $file_arr 返回值变量,当前目录的文件数组
# $path 指定要遍历的目录路径
# $start_str 只查询文件名以该字符串开始的文件
# $not_str 指定要排除的文件,多个以 || 分隔开
#######################################################
function view_folder(&$folder_arr, &$file_arr, $path = ".", $start_str = "", $not_str = ""){
if ( trim($path) == "" ) return $file_arr;
$start_str_len = strlen( trim($start_str) );
$not_str = ".||..||".$not_str;
$not_str_arr = explode( "||", trim($not_str) );
$handle=opendir($path);
while ($file = readdir($handle)) {
$path = realpath(str_replace('\\','/',$path));
if( !is_dir( $path."/".$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";
}
}
}else{
if ( $file <> "." ) $folder_arr[] = $file;
}
}
closedir($handle);
if ( count($folder_arr) > 0 ) sort($folder_arr); //排序数组
if ( count($file_arr) > 0 ) sort($file_arr); //排序数组
}
#######################################################
# 调用函数示例
#######################################################
//设置参数,并调用函数
$path = trim($_GET['path']); //获取当前目录路径
if( $path == "" ) $path = dirname(__FILE__);
$path = str_replace('\\','/',$path);
$start_str = ""; //只显示首字母等于此字符串的文件(此参数功能是用于特殊用途的,不需要请留空)
$not_str = ""; //要排除显示的文件名,多个以 || 分隔开(此参数功能是用于特殊用途的,不需要请留空)
view_folder($folder_arr, $file_arr, $path, $start_str, $not_str); //调用函数返回数组
$path_url = realpath($path); //将路径转换成实际有效的路径
echo "<b>当前目录:".$path_url."</b><br>";
//输出目录
if ( count($folder_arr) > 0 ){
foreach($folder_arr as $key => $value){
if ( trim($folder_arr[$key]) == ".." ){
echo "<div>[<a href='?path=".urlencode($path_url)."/".$folder_arr[$key]."'><font color=#000>".$folder_arr[$key]."</font></a>]</div>";
}else{
echo "<div>[<a href='?path=".urlencode($path_url)."/".urlencode($folder_arr[$key])."'><font color=#000>".$folder_arr[$key]."</font></a>]</div>";
}
}
}
//输出当前目录的文件
if ( count($file_arr) > 0 ){
$root_path = substr($_SERVER['DOCUMENT_ROOT'],0,strlen($_SERVER['DOCUMENT_ROOT'])-1); //根目录
$root_path_len = strlen($root_path);
$this_path = str_replace('\\','/',$path_url); //当前目录
$this_path_len = strlen($this_path);
if ( $root_path_len <= $this_path_len and substr($root_path,0,$root_path_len)==substr($this_path,0,$root_path_len) ){ //当在根目录范围内时,文件则以链接方式显示
$this_url = str_replace($root_path,"",$this_path); //去掉根目录绝对路径,使其以/开始
if ( $this_url == "" ){ //如果完全相等,则默认从根目录/开始
$this_url = "/";
}else{ //否则在目录后面加上“/”结尾,以连接文件名组成URL
$this_url .= "/";
}
//循环输出文件
foreach($file_arr as $key => $value){
echo "<div><a href='".$this_url.$file_arr[$key]."'target='_blank'>".$file_arr[$key]."</a></div>";
}
}else{ //当已超出根目录时,只显示文件,取消超链接
foreach($file_arr as $key => $value){
echo "<div>".$file_arr[$key]."</div>";
}
}
}
?>
文章来源:桂林唯创网络

