php列出目录所有文件名

2024年11月20日 20:35
有1个网友回答
网友(1):

php.ini
/*
Start Web Settings.
*/
define("TITLE","网站标题");
//Another method to define a constant.
//const TITLE = "网站标题";
//Start Web Settings End.
/*
Start MySQL Settings.
*/
$MySQL_HOST = "localhost";
$MySQL_PORT = "3306";
$MySQL_USER = "root";
$MySQL_PASSWORD = "";
$MySQL_DBNAME = "test";
//$MySQL = new pdo("mysql:dbname=".$MySQL_DBNAME.";host=".$MySQL_HOST,$MySQL_USER,$MySQL_PASSWORD);
//Start MySQL Settings End.
/*
Start Classes Settings.
*/
class Show_Files {
//获取绝对路径,未完善
function tr_directory($path_ = "/",$pre_path = "") {
if(strlen($pre_path) == 0){
$pre_path = getcwd();
}
if(is_dir($pre_path)){
if(is_dir($path_)){
$path = $path_;
}
else{
$path = $path_;
}
}
return $pre_path.$path_;
closedir($handle);
}
//获取路径中的所有文件名称,可排除部分类型的文件
function get_files($path = "./",$nofile = array()){
$source = scandir($path);
$files = array();
$out_ = array();
foreach($source as $file){
if(is_file($file)){
$files[] .= $file;
}
}
$out_ = preg_replace($nofile,"",$files);
$out = array();
foreach($out_ as $out_tmp){
if(strlen($out_tmp)>0){
$out[] .= $out_tmp;
}
}
return $out;
closedir($handle);
}
//获取路径中的所有目录名称
function get_dirs($path = "./",$nodir = array()){
$source = scandir($path);
$dirs = array();
foreach($source as $dir){
if(is_dir($dir)){
if(count($nodir) > 0){
foreach($nodir as $no_dir){
if(!preg_match($no_dir,$dir)){
$dirs[] .= $dir;
}
}
}
else{
$dirs[] .= $dir;
}
}
}
$dirs = preg_replace($nodir,"",$dirs);
$dirs_out = array();
foreach($dirs as $dir_tmp){
if(strlen($dir_tmp)>0){
$dirs_out[] .= $dir_tmp;
}
}
return $dirs_out;
closedir($handle);
}
function get_alias($files=array("/etc/apache2/apache2.conf","/etc/apache2/httpd.conf","/etc/apache2/sites-enabled/000-default","/etc/apache2/sites-enabled/000-default")){
$alias = array('name'=>array(),'dir'=>array(),'file'=>array());
foreach($files as $alias_file){
$res = shell_exec("cat ".$alias_file." | grep Alias");
$res = explode("\n",$res);
//去掉行首空白和tab符
foreach($res as $res){
if(strlen($res)>0){
$res = trim($res);
$res = explode(" ",$res);
$alias['file'][] .= $alias_file;
$alias['name'][] .= $res[1];
$alias['dir'][] .= $res[2];
}
}
}
return $alias;
}
}
//Start Classes Settings End.
?>
index.php
include_once("./ini.php");
?>




<?php echo TITLE." - 首页"; ?>






?>


站点导航




$f = new Show_Files;
//列出可访问目录及该目录下一级目录和文件
$dir = $f -> tr_directory("/");
echo "
";
$dirs = $f -> get_dirs($dir,array("/^\./"));
echo "

目录(已略去隐藏目录)

";
foreach($dirs as $dirs){
echo "

".$dirs."

";
}
//列出Alias配置的列表
echo "

Alias

";
$alias_file = array("/etc/apache2/apache2.conf","/etc/apache2/httpd.conf","/etc/apache2/sites-enabled/000-default");
$alias = $f -> get_alias($alias_file);
foreach($alias['name'] as $alia){
echo "".$alia."\n";
}
//列出可访问文件
echo "

文件(已略去隐藏文件、备份文件、配置文件)

";
$files = $f -> get_files($dir,array("/(.*)\.bak$/","/(.*)\~$/","/(.*)ini.php$/","/^\.(.*)/"));
foreach($files as $files){
echo "".$files."\n";
}
echo "
";
?>




自己写的提供出来看看,感兴趣的朋友可以一起讨论:haoyihuan@qq.com