Single index. php implements PHP folder traversal at any level (original Zjmainstay)

Source: Internet
Author: User

The following are core files:
Index. php file Copy codeThe Code is as follows: <? Php
Header ('content-Type: text/html charset: UTF-8 ');
Date_default_timezone_set ('prc ');
$ RootDir = 'listfile'; // the root directory of the site to load all files of the program.
// Site base_url setting method:
// Considering the versatility, method 2 is used by default. When modifying the method, you must modify the. htaccess file at the same time.
// Method 1: Set the site directory as the root directory
// Corresponds to. htaccess:
// # RewriteBase/
// $ Base_url = 'HTTP: // www.listfile.com /';
// Method 2: Set the site subdirectory as the root directory
// Corresponds to. htaccess:
// RewriteBase/listFile/
$ Base_url = 'HTTP: // www.test.com/'. $ rootDir .'/';
// Parse the folder path
If (empty ($ _ GET ['Return ']) {
$ Dir = '.';
} Else {
$ Dir = trim (array_pop (explode ($ rootDir, $ _ GET ['Return ']),'/');
If (empty ($ dir) $ dir = '.';
Else $ dir = './'. $ dir;
}
// Echo $ dir; // current folder
// Traverse the current folder
$ Pattern = '*'; // '*' searches for all files, which can be intelligently matched, such *. jpg search for jpg files ,*. {jpg, png} searches for jpg and png files, case sensitive !!
$ Skip = '*. skip'; // exclude. skip type file (corresponding to "skipped output file. "), you can modify it yourself, such *. exclude all php files in 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?> ';
// The Link carries the return flag. If it carries the return flag, the autoClickUrl adds a lower-level folder for jump. After the jump, the URL is beautified.
Var autoClickUrl = '<? Php echo (strpos ($ _ SERVER ['request _ URI '],'? Return ')! = False )? Array_shift (explode ('? ', $ _ SERVER ['request _ URI']). "baddir/": '';?> ';
</Script>
<? Php
// Array of file types
$ Filetypes = array (
'Txt '=> 'txt text file ',
'Dir' => 'Folder ',
'Php' => 'php file ',
'Css '=> 'css file ',
'Js' => 'js files ',
'Doc' => 'word document ',
'Xls '=> 'excel worksheet ',
'Jpg '=> 'jpg image file ',
'Gif' => 'gif image file ',
'Png '=> 'png image file ',
'Mp3' => 'mp3 file ',
'Zip' => 'zip package ',
'Rar '=> 'rar compressed package ',
'Htm '=> 'htm webpage file ',
'Html '=> 'html webpage file ',
'Undefined' => 'file type unknown ',
);
// Customize the shielded output file
$ Skipfiles = array (
'Index. php ',
'Index.html ',
'Jquery-1.6.2.min.js ',
'Main. js ',
'Base.css ',
);
// Output all files in the current folder according to the rule
Echo "<div id = 'back'> <a href =''> .. </a> </div> ";
Echo "<div id = 'Container'> ";
Echo "<div class = 'file text-Center'> <div class = 'filename border-Right'> name </div> <div class = 'filesize border-Right'> size </div> ";
Echo "<div class = 'filetype border-Right'> type </div> <div class = 'filemtime'> modification date </div> ";
Foreach ($ files ['filename'] as $ index => $ file ){
If (in_array ($ file, $ skipfiles) continue;
If (is_null ($ filetypes [$ files ['text'] [$ index]) $ filetype = 'file type unknown ';
Else $ filetype = $ filetypes [$ files ['text'] [$ index];
Echo "<div class = 'file'> <div class = 'filename'> <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> ";
}
Echo '</div> ';
?>
</Body>
</Html>
<? Php
// Folder traversal Function
Function scandir_through ($ dir, $ pattern = '*', $ skip = false, $ subInclude = true, $ flag = GLOB_BRACE ){
$ Files = array ();
// Obtain all files and folders in the current directory
$ Items = glob ($ dir .'/*');
// Traverse all items. If $ subInclude is set to true, continue to traverse sub-directories.
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 );
// If the current file matches the file search mode $ pattern, add it to the $ files array.
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]); // put it here to solve the problem that the retrieval time of the Chinese name file after iconv fails.
$ 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 ['text'] [] = 'dir ';
$ Files ['filesize'] [] = '';
} Else {
$ Files ['filesize'] [] = ceil (filesize ($ file)/1024). 'kb ';
If (strrpos ($ file, '.') = false) $ files ['text'] [] = 'undefined ';
Else $ files ['ext '] [] = strtolower (array_pop (explode ('. ', $ file )));
}
}
}
}
Return $ files;
}
/*
// The. htaccess file is located in the root directory. Principle: The access path is not a file, that is, a folder. Therefore, the file is redirected to the root path for parsing.
RewriteEngine on
# Level-1 directory method
# RewriteBase/
# List Directory Method
RewriteBase/listFile/
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule (. *) index. php? Return = % {REQUEST_FILENAME} [L]
*/
?>

JS filesCopy codeThe Code is as follows: $ (document). ready (function (){
// Return link for Root Node Deletion
If (window. location. href = base_url) $ ("# back"). hide ();
// Return Processing
$ ("# 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; // if a link is triggered at the root node, the link is returned directly.
Url = url. replace (location. search, ''); // if the link carries? Return: removes the subsequent content of return (generated by. htaccess)
Url = url. substr (0, url. length-2); // start from the last 2nd bits of the url to avoid a jump error when the/# condition exists
Url = url. substr (0, url. lastIndexOf ('/') + 1); // remove the last layer of folders and move back to the next level
Window. location. href = url;
Return false; // after processing is complete, false is returned to prevent the jump after the <a> tag is clicked.
});
If (autoClickUrl! = '') $ (" # Back a "). click ()
});

CSS fileCopy codeThe Code is as follows: # container {
Border: 1px solid;
Margin: 0 auto;
Padding: 10px;
Width: 654px;
Border-radius: 10px 10px 10px;
}
# Back {
Width: 654px;
Margin: 0 auto;
}
# Back {
Line-style: none;
}
. File {
Clear: both;
Height: 2px;
Margin-bottom: 20px;
}
. File img {
Float: left;
}
. File {
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 FileCopy codeThe Code is as follows: # principle: the access path is not a file, that is, a folder. Therefore, the system redirects to the root path for parsing and listing all the files in the current directory.
RewriteEngine on
# Level-1 directory method
# RewriteBase/
# List Directory Method
RewriteBase/listFile/
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule (. *) index. php? Return = % {REQUEST_FILENAME} [L]

Core Folder: listFile/images/
As follows:
// ListFile


Package download: Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.