單一index.php實(shí)現(xiàn)PHP任意層級(jí)文件夾遍歷(Zjmainstay原創(chuàng))
更新時(shí)間:2012年07月31日 23:15:53 作者:
本程序?qū)崿F(xiàn)了使用一個(gè)index.php文件來(lái)實(shí)現(xiàn)所有文件夾的遍歷效果,避免了需要無(wú)窮復(fù)制index.php至文件夾下才能實(shí)現(xiàn)的效果
以下是核心文件:
index.php文件
<?php
header('Content-Type:text/html charset:utf-8');
date_default_timezone_set('PRC');
$rootDir = 'listFile'; //站點(diǎn)根目錄,裝載本程序所有文件
//站點(diǎn)base_url設(shè)置方法:
//考慮到通用性,現(xiàn)默認(rèn)使用方法二,修改方法時(shí)注意同時(shí)修改.htaccess文件
//方法一:設(shè)置站點(diǎn)目錄為根目錄
//對(duì)應(yīng).htaccess:
//#RewriteBase /
// $base_url = 'http://www.listfile.com/';
//方法二:設(shè)置站點(diǎn)子目錄為根目錄
//對(duì)應(yīng).htaccess:
//RewriteBase /listFile/
$base_url = 'http://www.test.com/' .$rootDir .'/';
//解析文件夾路徑
if(empty($_GET['return'])){
$dir = '.';
}else {
$dir = trim(array_pop(explode($rootDir,$_GET['return'])),'/');
if(empty($dir)) $dir = '.';
else $dir = './' . $dir;
}
// echo $dir; //當(dāng)前文件夾
//遍歷當(dāng)前文件夾
$pattern = '*'; // '*'搜索全部文件,可以智能匹配,如*.jpg 搜索jpg文件,*.{jpg,png}搜索jpg和png文件,區(qū)分大小寫?。?
$skip = '*.skip'; //排除.skip類型文件(對(duì)應(yīng)了“被跳過(guò)輸出文件.skip”),你可以自己修改,如*.php排除所有php文件
$files = scandir_through($dir,$pattern,$skip,false);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>List Files</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<script type="text/javascript" src="<?php echo $base_url . 'jquery-1.6.2.min.js' ?>"></script>
<script type="text/javascript" src="<?php echo $base_url . 'main.js' ?>"></script>
<link rel="stylesheet" rev="stylesheet" href="<?php echo $base_url . 'base.css' ?>" type="text/css" />
</head>
<body>
<script type="text/javascript">
var base_url = '<?php echo $base_url ?>';
//鏈接攜帶return標(biāo)志,若攜帶,則autoClickUrl自添加一層下級(jí)文件夾用于跳轉(zhuǎn),跳轉(zhuǎn)后獲得美化后的URL。
var autoClickUrl = '<?php echo (strpos($_SERVER['REQUEST_URI'],'?return') !== false)?array_shift(explode('?',$_SERVER['REQUEST_URI']))."baddir/":'';?>';
</script>
<?php
//文件類型數(shù)組
$filetypes = array(
'txt' => 'txt文本文件',
'dir' => '文件夾',
'php' => 'php文件',
'css' => 'css文件',
'js' => 'js文件',
'doc' => 'Word文檔',
'xls' => 'Excel工作表',
'jpg' => 'jpg圖片文件',
'gif' => 'gif圖片文件',
'png' => 'png圖片文件',
'mp3' => 'mp3文件',
'zip' => 'zip壓縮包',
'rar' => 'rar壓縮包',
'htm' => 'htm網(wǎng)頁(yè)文件',
'html' => 'html網(wǎng)頁(yè)文件',
'undefined'=>'文件類型未知',
);
//自定義屏蔽輸出文件
$skipfiles = array(
'index.php',
'index.html',
'jquery-1.6.2.min.js',
'main.js',
'base.css',
);
//按規(guī)律輸出當(dāng)前文件夾所有文件
echo "<div id='back'><a href=''><img src='{$base_url}images/dir.jpg'/>..</a></div>";
echo "<div id='container'>";
echo "<div class='file text-center'><div class='filename border-right'>名稱</div><div class='filesize border-right'>大小</div>";
echo "<div class='filetype border-right'>類型</div><div class='filemtime'>修改日期</div></div>";
foreach($files['filename'] as $index => $file){
if(in_array($file,$skipfiles)) continue;
if(is_null($filetypes[$files['ext'][$index]])) $filetype = '文件類型未知';
else $filetype = $filetypes[$files['ext'][$index]];
echo "<div class='file'><div class='filename'><img src='{$base_url}images/{$files['ext'][$index]}.jpg'/><a href='{$base_url}{$files['widthDir'][$index]}'>{$file}</a></div>";
echo "<div class='filesize text-right'>{$files['filesize'][$index]} </div><div class='filetype text-right'>{$filetype}</div>";
echo "<div class='filemtime text-center'>{$files['filemtime'][$index]}</div></div>";
}
echo '</div>';
?>
</body>
</html>
<?php
//文件夾遍歷函數(shù)
function scandir_through($dir,$pattern='*',$skip=false,$subInclude=true,$flag=GLOB_BRACE){
$files = array();
//獲取當(dāng)前目錄下所有文件及文件夾
$items = glob($dir . '/*');
//遍歷所有項(xiàng)目,若設(shè)置$subInclude為true,則繼續(xù)遍歷子目錄
for ($i = 0; $i < count($items); $i++) {
if ($subInclude && is_dir($items[$i])) {
$add = glob($items[$i] . '/*');
if($add === false) $add = array();
$items = array_merge($items, $add);
}else {
$slash = strrpos($items[$i],'/');
$dir = substr($items[$i],0,$slash);
//若當(dāng)前文件匹配文件查找模式$pattern,則加入$files數(shù)組中
if(in_array($items[$i],glob($dir.'/'.$pattern,$flag)) && (($skip===false) || !in_array($items[$i],glob($dir.'/'.$skip,$flag)))) {
$files['filemtime'][] = date('Y-m-d H:i:s',filemtime($items[$i])); //放這里為了解決iconv后中文名文件獲取時(shí)間失敗問題
$items[$i] = iconv('gb2312','utf-8',$items[$i]);
$file = substr($items[$i],$slash+1);
$files['widthDir'][] = $items[$i];
$files['filename'][] = $file;
if(is_dir($items[$i])) {
$files['ext'][] = 'dir';
$files['filesize'][] = '';
}else {
$files['filesize'][] = ceil(filesize($file)/1024).'KB';
if(strrpos($file,'.') === false) $files['ext'][] = 'undefined';
else $files['ext'][] = strtolower(array_pop(explode('.',$file)));
}
}
}
}
return $files;
}
/*
//.htaccess 文件,位于根目錄下,原理:訪問路徑非文件,即文件夾,因此跳轉(zhuǎn)至根路徑下做解析。
RewriteEngine on
#一級(jí)目錄法
#RewriteBase /
#二級(jí)目錄法
RewriteBase /listFile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?return=%{REQUEST_FILENAME} [L]
*/
?>
JS文件
$(document).ready(function(){
//根節(jié)點(diǎn)刪除返回鏈接
if(window.location.href == base_url) $("#back").hide();
//返回處理
$("#back a").click(function(){
if(autoClickUrl != ''){
//Add baddir for click back.
var url = autoClickUrl;
}else{
var url=window.location.href;
}
if(url == base_url) return false; //如果在根節(jié)點(diǎn)觸發(fā)返回鏈接,直接返回。
url = url.replace(location.search,''); //如果鏈接攜帶?return,截除return后續(xù)內(nèi)容(由.htaccess生成)
url = url.substr(0,url.length-2); // 從url后第2位開始,避免/#情況存在時(shí)跳轉(zhuǎn)錯(cuò)誤
url = url.substr(0,url.lastIndexOf('/')+1); //截除最后一層文件夾,后退一級(jí)
window.location.href = url;
return false; //處理完畢,返回false阻止<a>標(biāo)簽點(diǎn)擊后的跳轉(zhuǎn)。
});
if(autoClickUrl != '') $("#back a").click()
});
CSS文件
#container{
border: 1px solid;
margin: 0 auto;
padding: 10px;
width: 654px;
border-radius: 10px 10px 10px 10px;
}
#back{
width: 654px;
margin: 0 auto;
}
#back a{
line-style:none;
}
.file{
clear: both;
height: 2px;
margin-bottom: 20px;
}
.file img{
float:left;
}
.file a{
float:left;
margin-left: 5px;
}
.file div{
float:left;
width:150px;
}
.text-left{
text-align:left;
}
.text-center{
text-align:center;
}
.text-right{
text-align:right;
}
.border-left{
border-left:1px solid;
}
.border-right{
border-right:1px solid;
}
.file div.filename{
width:200px;
}
.file div.filesize{
width:100px;
}
.file div.filemtime{
width:200px;
}
.htaccess文件
#原理:訪問路徑非文件,即文件夾,因此跳轉(zhuǎn)至根路徑下做解析獲取當(dāng)前目錄下的所有文件并列出。
RewriteEngine on
#一級(jí)目錄法
#RewriteBase /
#二級(jí)目錄法
RewriteBase /listFile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?return=%{REQUEST_FILENAME} [L]
核心文件夾:listFile/images/
效果圖如下:
//listFile
index.php文件
復(fù)制代碼 代碼如下:
<?php
header('Content-Type:text/html charset:utf-8');
date_default_timezone_set('PRC');
$rootDir = 'listFile'; //站點(diǎn)根目錄,裝載本程序所有文件
//站點(diǎn)base_url設(shè)置方法:
//考慮到通用性,現(xiàn)默認(rèn)使用方法二,修改方法時(shí)注意同時(shí)修改.htaccess文件
//方法一:設(shè)置站點(diǎn)目錄為根目錄
//對(duì)應(yīng).htaccess:
//#RewriteBase /
// $base_url = 'http://www.listfile.com/';
//方法二:設(shè)置站點(diǎn)子目錄為根目錄
//對(duì)應(yīng).htaccess:
//RewriteBase /listFile/
$base_url = 'http://www.test.com/' .$rootDir .'/';
//解析文件夾路徑
if(empty($_GET['return'])){
$dir = '.';
}else {
$dir = trim(array_pop(explode($rootDir,$_GET['return'])),'/');
if(empty($dir)) $dir = '.';
else $dir = './' . $dir;
}
// echo $dir; //當(dāng)前文件夾
//遍歷當(dāng)前文件夾
$pattern = '*'; // '*'搜索全部文件,可以智能匹配,如*.jpg 搜索jpg文件,*.{jpg,png}搜索jpg和png文件,區(qū)分大小寫?。?
$skip = '*.skip'; //排除.skip類型文件(對(duì)應(yīng)了“被跳過(guò)輸出文件.skip”),你可以自己修改,如*.php排除所有php文件
$files = scandir_through($dir,$pattern,$skip,false);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>List Files</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<script type="text/javascript" src="<?php echo $base_url . 'jquery-1.6.2.min.js' ?>"></script>
<script type="text/javascript" src="<?php echo $base_url . 'main.js' ?>"></script>
<link rel="stylesheet" rev="stylesheet" href="<?php echo $base_url . 'base.css' ?>" type="text/css" />
</head>
<body>
<script type="text/javascript">
var base_url = '<?php echo $base_url ?>';
//鏈接攜帶return標(biāo)志,若攜帶,則autoClickUrl自添加一層下級(jí)文件夾用于跳轉(zhuǎn),跳轉(zhuǎn)后獲得美化后的URL。
var autoClickUrl = '<?php echo (strpos($_SERVER['REQUEST_URI'],'?return') !== false)?array_shift(explode('?',$_SERVER['REQUEST_URI']))."baddir/":'';?>';
</script>
<?php
//文件類型數(shù)組
$filetypes = array(
'txt' => 'txt文本文件',
'dir' => '文件夾',
'php' => 'php文件',
'css' => 'css文件',
'js' => 'js文件',
'doc' => 'Word文檔',
'xls' => 'Excel工作表',
'jpg' => 'jpg圖片文件',
'gif' => 'gif圖片文件',
'png' => 'png圖片文件',
'mp3' => 'mp3文件',
'zip' => 'zip壓縮包',
'rar' => 'rar壓縮包',
'htm' => 'htm網(wǎng)頁(yè)文件',
'html' => 'html網(wǎng)頁(yè)文件',
'undefined'=>'文件類型未知',
);
//自定義屏蔽輸出文件
$skipfiles = array(
'index.php',
'index.html',
'jquery-1.6.2.min.js',
'main.js',
'base.css',
);
//按規(guī)律輸出當(dāng)前文件夾所有文件
echo "<div id='back'><a href=''><img src='{$base_url}images/dir.jpg'/>..</a></div>";
echo "<div id='container'>";
echo "<div class='file text-center'><div class='filename border-right'>名稱</div><div class='filesize border-right'>大小</div>";
echo "<div class='filetype border-right'>類型</div><div class='filemtime'>修改日期</div></div>";
foreach($files['filename'] as $index => $file){
if(in_array($file,$skipfiles)) continue;
if(is_null($filetypes[$files['ext'][$index]])) $filetype = '文件類型未知';
else $filetype = $filetypes[$files['ext'][$index]];
echo "<div class='file'><div class='filename'><img src='{$base_url}images/{$files['ext'][$index]}.jpg'/><a href='{$base_url}{$files['widthDir'][$index]}'>{$file}</a></div>";
echo "<div class='filesize text-right'>{$files['filesize'][$index]} </div><div class='filetype text-right'>{$filetype}</div>";
echo "<div class='filemtime text-center'>{$files['filemtime'][$index]}</div></div>";
}
echo '</div>';
?>
</body>
</html>
<?php
//文件夾遍歷函數(shù)
function scandir_through($dir,$pattern='*',$skip=false,$subInclude=true,$flag=GLOB_BRACE){
$files = array();
//獲取當(dāng)前目錄下所有文件及文件夾
$items = glob($dir . '/*');
//遍歷所有項(xiàng)目,若設(shè)置$subInclude為true,則繼續(xù)遍歷子目錄
for ($i = 0; $i < count($items); $i++) {
if ($subInclude && is_dir($items[$i])) {
$add = glob($items[$i] . '/*');
if($add === false) $add = array();
$items = array_merge($items, $add);
}else {
$slash = strrpos($items[$i],'/');
$dir = substr($items[$i],0,$slash);
//若當(dāng)前文件匹配文件查找模式$pattern,則加入$files數(shù)組中
if(in_array($items[$i],glob($dir.'/'.$pattern,$flag)) && (($skip===false) || !in_array($items[$i],glob($dir.'/'.$skip,$flag)))) {
$files['filemtime'][] = date('Y-m-d H:i:s',filemtime($items[$i])); //放這里為了解決iconv后中文名文件獲取時(shí)間失敗問題
$items[$i] = iconv('gb2312','utf-8',$items[$i]);
$file = substr($items[$i],$slash+1);
$files['widthDir'][] = $items[$i];
$files['filename'][] = $file;
if(is_dir($items[$i])) {
$files['ext'][] = 'dir';
$files['filesize'][] = '';
}else {
$files['filesize'][] = ceil(filesize($file)/1024).'KB';
if(strrpos($file,'.') === false) $files['ext'][] = 'undefined';
else $files['ext'][] = strtolower(array_pop(explode('.',$file)));
}
}
}
}
return $files;
}
/*
//.htaccess 文件,位于根目錄下,原理:訪問路徑非文件,即文件夾,因此跳轉(zhuǎn)至根路徑下做解析。
RewriteEngine on
#一級(jí)目錄法
#RewriteBase /
#二級(jí)目錄法
RewriteBase /listFile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?return=%{REQUEST_FILENAME} [L]
*/
?>
JS文件
復(fù)制代碼 代碼如下:
$(document).ready(function(){
//根節(jié)點(diǎn)刪除返回鏈接
if(window.location.href == base_url) $("#back").hide();
//返回處理
$("#back a").click(function(){
if(autoClickUrl != ''){
//Add baddir for click back.
var url = autoClickUrl;
}else{
var url=window.location.href;
}
if(url == base_url) return false; //如果在根節(jié)點(diǎn)觸發(fā)返回鏈接,直接返回。
url = url.replace(location.search,''); //如果鏈接攜帶?return,截除return后續(xù)內(nèi)容(由.htaccess生成)
url = url.substr(0,url.length-2); // 從url后第2位開始,避免/#情況存在時(shí)跳轉(zhuǎn)錯(cuò)誤
url = url.substr(0,url.lastIndexOf('/')+1); //截除最后一層文件夾,后退一級(jí)
window.location.href = url;
return false; //處理完畢,返回false阻止<a>標(biāo)簽點(diǎn)擊后的跳轉(zhuǎn)。
});
if(autoClickUrl != '') $("#back a").click()
});
CSS文件
復(fù)制代碼 代碼如下:
#container{
border: 1px solid;
margin: 0 auto;
padding: 10px;
width: 654px;
border-radius: 10px 10px 10px 10px;
}
#back{
width: 654px;
margin: 0 auto;
}
#back a{
line-style:none;
}
.file{
clear: both;
height: 2px;
margin-bottom: 20px;
}
.file img{
float:left;
}
.file a{
float:left;
margin-left: 5px;
}
.file div{
float:left;
width:150px;
}
.text-left{
text-align:left;
}
.text-center{
text-align:center;
}
.text-right{
text-align:right;
}
.border-left{
border-left:1px solid;
}
.border-right{
border-right:1px solid;
}
.file div.filename{
width:200px;
}
.file div.filesize{
width:100px;
}
.file div.filemtime{
width:200px;
}
.htaccess文件
復(fù)制代碼 代碼如下:
#原理:訪問路徑非文件,即文件夾,因此跳轉(zhuǎn)至根路徑下做解析獲取當(dāng)前目錄下的所有文件并列出。
RewriteEngine on
#一級(jí)目錄法
#RewriteBase /
#二級(jí)目錄法
RewriteBase /listFile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?return=%{REQUEST_FILENAME} [L]
核心文件夾:listFile/images/
效果圖如下:
//listFile
軟件包下載:下載
您可能感興趣的文章:
- php遍歷文件夾和文件列表示例分享
- php獲取文件夾路徑內(nèi)的圖片以及分頁(yè)顯示示例
- php 創(chuàng)建以UNIX時(shí)間戳命名的文件夾(示例代碼)
- php 生成自動(dòng)創(chuàng)建文件夾并上傳文件的示例代碼
- php無(wú)限遍歷文件夾示例分享
- php顯示當(dāng)前文件所在的文件以及文件夾所有文件以樹形展開
- php遍歷文件夾所有文件子文件夾函數(shù)代碼
- php遍歷目錄與文件夾的多種方法詳解
- php文件夾與文件目錄操作函數(shù)介紹
- PHP遍歷某個(gè)目錄下的所有文件和子文件夾的實(shí)現(xiàn)代碼
- 探討PHP刪除文件夾的三種方法
- php遍歷所有文件及文件夾的方法深入解析
- PHP轉(zhuǎn)換文件夾下所有文件編碼的實(shí)現(xiàn)代碼
- IIS PHP環(huán)境Temp文件夾的權(quán)限問題引起的網(wǎng)站故障
- php刪除與復(fù)制文件夾及其文件夾下所有文件的實(shí)現(xiàn)代碼
- php刪除文件夾及其文件夾下所有文件的函數(shù)代碼
- php定時(shí)刪除文件夾下文件(清理緩存文件)
- php遍歷文件夾下的所有文件和子文件夾示例
相關(guān)文章
php制作動(dòng)態(tài)隨機(jī)驗(yàn)證碼
這篇文章主要介紹了php制作動(dòng)態(tài)隨機(jī)驗(yàn)證碼的方法的相關(guān)資料,需要的朋友可以參考下2015-02-02php+jQuery.uploadify實(shí)現(xiàn)文件上傳教程
這篇文章主要介紹了php+jQuery.uploadify實(shí)現(xiàn)文件上傳教程,需要的朋友可以參考下2014-12-12php通過(guò)PHPExcel導(dǎo)入Excel表格到MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇php通過(guò)PHPExcel導(dǎo)入Excel表格到MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10Laravel 不同生產(chǎn)環(huán)境服務(wù)器的判斷實(shí)踐
這篇文章主要介紹了Laravel 不同生產(chǎn)環(huán)境服務(wù)器的判斷實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10YII2框架中操作數(shù)據(jù)庫(kù)的方式實(shí)例分析
這篇文章主要介紹了YII2框架中操作數(shù)據(jù)庫(kù)的方式,結(jié)合實(shí)例形式總結(jié)分析了YII2使用createCommand方式及AR(Active Record)方式操作數(shù)據(jù)庫(kù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-03-03Yii實(shí)現(xiàn)簡(jiǎn)單分頁(yè)的方法
這篇文章主要介紹了Yii實(shí)現(xiàn)簡(jiǎn)單分頁(yè)的方法,涉及Yii模型調(diào)用讀取數(shù)據(jù)及視圖操作相關(guān)技巧,需要的朋友可以參考下2016-04-04