單一index.php實現PHP任意層級檔案夾遍曆(Zjmainstay原創)_PHP教程

來源:互聯網
上載者:User
以下是核心檔案:
index.php檔案
複製代碼 代碼如下:
header('Content-Type:text/html charset:utf-8');
date_default_timezone_set('PRC');
$rootDir = 'listFile'; //網站根目錄,裝載本程式所有檔案
//網站base_url設定方法:
//考慮到通用性,現預設使用方法二,修改方法時注意同時修改.htaccess檔案
//方法一:佈建網站目錄為根目錄
//對應.htaccess:
//#RewriteBase /
// $base_url = 'http://www.listfile.com/';
//方法二:佈建網站子目錄為根目錄
//對應.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; //當前檔案夾
//遍曆當前檔案夾
$pattern = '*'; // '*'搜尋全部檔案,可以智能匹配,如*.jpg 搜尋jpg檔案,*.{jpg,png}搜尋jpg和png檔案,區分大小寫!!
$skip = '*.skip'; //排除.skip類型檔案(對應了“被跳過輸出檔案.skip”),你可以自己修改,如*.php排除所有php檔案
$files = scandir_through($dir,$pattern,$skip,false);
?>



List Files








//檔案類型數組
$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網頁檔案',
'html' => 'html網頁檔案',
'undefined'=>'檔案類型未知',
);
//自訂屏蔽輸出檔案
$skipfiles = array(
'index.php',
'index.html',
'jquery-1.6.2.min.js',
'main.js',
'base.css',
);
//按規律輸出當前檔案夾所有檔案
echo "..";
echo "";
echo "名稱大小";
echo "類型修改日期";
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 "{$file}";
echo "{$files['filesize'][$index]} {$filetype}";
echo "{$files['filemtime'][$index]}";
}
echo '';
?>


//檔案夾遍曆函數
function scandir_through($dir,$pattern='*',$skip=false,$subInclude=true,$flag=GLOB_BRACE){
$files = array();
//擷取目前的目錄下所有檔案及檔案夾
$items = glob($dir . '/*');
//遍曆所有項目,若設定$subInclude為true,則繼續遍曆子目錄
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);
//若當前檔案匹配檔案尋找模式$pattern,則加入$files數組中
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後中文名檔案擷取時間失敗問題
$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 檔案,位於根目錄下,原理:訪問路徑非檔案,即檔案夾,因此跳轉至根路徑下做解析。
RewriteEngine on
#一級目錄法
#RewriteBase /
#二級目錄法
RewriteBase /listFile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?return=%{REQUEST_FILENAME} [L]
*/
?>

JS檔案
複製代碼 代碼如下:
$(document).ready(function(){
//根節點刪除返回連結
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; //如果在根節點觸發返回連結,直接返回。
url = url.replace(location.search,''); //如果連結攜帶?return,截除return後續內容(由.htaccess產生)
url = url.substr(0,url.length-2); // 從url後第2位開始,避免/#情況存在時跳轉錯誤
url = url.substr(0,url.lastIndexOf('/')+1); //截除最後一層檔案夾,後退一級
window.location.href = url;
return false; //處理完畢,返回false阻止標籤點擊後的跳轉。
});
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檔案
複製代碼 代碼如下:
#原理:訪問路徑非檔案,即檔案夾,因此跳轉至根路徑下做解析擷取目前的目錄下的所有檔案並列出。
RewriteEngine on
#一級目錄法
#RewriteBase /
#二級目錄法
RewriteBase /listFile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?return=%{REQUEST_FILENAME} [L]

核心檔案夾:listFile/images/
如下:
//listFile


軟體包下載:下載

http://www.bkjia.com/PHPjc/325787.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325787.htmlTechArticle以下是核心檔案: index.php檔案 複製代碼 代碼如下: ?php header('Content-Type:text/html charset:utf-8'); date_default_timezone_set('PRC'); $rootDir = 'listFile'; //站...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.