php列出目录所有文件名
如何用php列出一个母文件夹下所有子文件夹中所有的文件名。比如test目录中test1、test2,test3,test4文件夹中的所有文件名。谢谢...
如何用php列出一个母文件夹下所有子文件夹中所有的文件名。比如test目录中test1、test2,test3,test4文件夹中的所有文件名。谢谢
展开
1个回答
展开全部
php.ini
<?php
/*
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
<?php
include_once("./ini.php");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="en" xml:lang="en">
<head>
<title><?php echo TITLE." - 首页"; ?></title>
<meta http-equiv="Content-Type" content="txt/html; charset=utf-8" />
<style type="text/css">
html{
background: #ddd;
}
body {
margin: 1em 10%;
padding: 1em 3em;
font: 80%/1.4 tahoma, arial, helvetica, lucida sans, sans-serif;
border: 1px solid #999;
background: #eee;
position: relative;
}
h2 {
margin: 0.8em 0 0 0;
}
</style>
<link rel="shortcut icon" href="index.php?img=favicon" type="./image/ico" />
</head>
<body>
<?php
?>
<div>
<h2 align="center">站点导航</h2>
</div>
<div>
<?php
$f = new Show_Files;
//列出可访问目录及该目录下一级目录和文件
$dir = $f -> tr_directory("/");
echo "<pre>";
$dirs = $f -> get_dirs($dir,array("/^\./"));
echo "<p><span style=\"font-size:14px;font-weight:bold\">目录</span>(已略去隐藏目录)</p>";
foreach($dirs as $dirs){
echo "<p><a href=\"".$dirs."\">".$dirs."</a></p>";
}
//列出Alias配置的列表
echo "<p><span style=\"font-size:14px;font-weight:bold\">Alias</span></p>";
$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 "<a href=\"".$alia."\">".$alia."</a>\n";
}
//列出可访问文件
echo "<p><span style=\"font-size:14px;font-weight:bold\">文件</span>(已略去隐藏文件、备份文件、配置文件)</p>";
$files = $f -> get_files($dir,array("/(.*)\.bak$/","/(.*)\~$/","/(.*)ini.php$/","/^\.(.*)/"));
foreach($files as $files){
echo "<a href=\"".$files."\">".$files."</a>\n";
}
echo "</pre>";
?>
</div>
</body>
</html>
自己写的提供出来看看,感兴趣的朋友可以一起讨论:haoyihuan@qq.com
<?php
/*
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
<?php
include_once("./ini.php");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="en" xml:lang="en">
<head>
<title><?php echo TITLE." - 首页"; ?></title>
<meta http-equiv="Content-Type" content="txt/html; charset=utf-8" />
<style type="text/css">
html{
background: #ddd;
}
body {
margin: 1em 10%;
padding: 1em 3em;
font: 80%/1.4 tahoma, arial, helvetica, lucida sans, sans-serif;
border: 1px solid #999;
background: #eee;
position: relative;
}
h2 {
margin: 0.8em 0 0 0;
}
</style>
<link rel="shortcut icon" href="index.php?img=favicon" type="./image/ico" />
</head>
<body>
<?php
?>
<div>
<h2 align="center">站点导航</h2>
</div>
<div>
<?php
$f = new Show_Files;
//列出可访问目录及该目录下一级目录和文件
$dir = $f -> tr_directory("/");
echo "<pre>";
$dirs = $f -> get_dirs($dir,array("/^\./"));
echo "<p><span style=\"font-size:14px;font-weight:bold\">目录</span>(已略去隐藏目录)</p>";
foreach($dirs as $dirs){
echo "<p><a href=\"".$dirs."\">".$dirs."</a></p>";
}
//列出Alias配置的列表
echo "<p><span style=\"font-size:14px;font-weight:bold\">Alias</span></p>";
$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 "<a href=\"".$alia."\">".$alia."</a>\n";
}
//列出可访问文件
echo "<p><span style=\"font-size:14px;font-weight:bold\">文件</span>(已略去隐藏文件、备份文件、配置文件)</p>";
$files = $f -> get_files($dir,array("/(.*)\.bak$/","/(.*)\~$/","/(.*)ini.php$/","/^\.(.*)/"));
foreach($files as $files){
echo "<a href=\"".$files."\">".$files."</a>\n";
}
echo "</pre>";
?>
</div>
</body>
</html>
自己写的提供出来看看,感兴趣的朋友可以一起讨论:haoyihuan@qq.com
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |