Single index.php implementation of PHP arbitrary level folder traversal (Zjmainstay original) _php instance

Source: Internet
Author: User
Tags explode glob rar zip
The following are the core files:
index.php file
Copy Code code as follows:

<?php
Header (' content-type:text/html charset:utf-8 ');
Date_default_timezone_set (' PRC ');
$rootDir = ' listfile '; Site root directory, loading all files of this program
Site Base_url Settings Method:
Taking into account versatility, the default is to use method two, modify the method to pay attention to simultaneously modify the. htaccess file
Method One: Set the Site Directory as the root directory
Correspondence. Htaccess:
#RewriteBase/
$base _url = ' http://www.listfile.com/';
Method Two: Set the site subdirectory to be the root directory
Correspondence. Htaccess:
rewritebase/listfile/
$base _url = ' http://www.test.com/'. $rootDir. ' /';
Resolving folder paths
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 = ' * '; ' * ' Search all files, can be smart match, such as *.jpg search jpg files, *. {jpg,png} search for JPG and PNG files, case-sensitive!!
$skip = ' *.skip '; Exclude. Skip type file (corresponding to "skipped output file. Skip"), you can modify it yourself, such as *.php Exclude all PHP files
$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 ">
<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 '/>
<body>
<script type= "Text/javascript" >
var base_url = ' <?php echo $base _url?> ';
Link with return flag, if carried, then Autoclickurl from adding a layer of subordinate folders for jump, after the jump to get landscaping after the URL.
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 file ',
' Doc ' => ' Word document ',
' xls ' => ' Excel worksheet ',
' jpg ' => ' jpg picture file ',
' gif ' => ' gif image file ',
' PNG ' => ' png picture file ',
' MP3 ' => ' mp3 file ',
' Zip ' => ' zip compression package ',
' rar ' => ' rar compressed package ',
' htm ' => ' htm web file ',
' HTML ' => ' HTML Web page file ',
' Undefined ' => ' file type unknown ',
);
Custom Masking Output File
$skipfiles = Array (
' index.php ',
' Index.html ',
' Jquery-1.6.2.min.js ',
' Main.js ',
' Base.css ',
);
Output current folder All files by law
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 ' > Modified date </div></div > ";
foreach ($files [' filename '] as $index => $file) {
if (In_array ($file, $skipfiles)) continue;
if (Is_null ($filetypes [$files [' ext '] [$index]]) $filetype = ' file type unknown ';
else $filetype = $filetypes [$files [' ext '] [$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></div>";
}
Echo ' </div> ';
?>
</body>
<?php
Folder Traversal function
function Scandir_through ($dir, $pattern = ' * ', $skip =false, $subInclude =true, $flag =glob_brace) {
$files = Array ();
Get all files and folders under current directory
$items = Glob ($dir. '/*');
Iterate through all the items, and if you set $subinclude to True, continue to traverse the subdirectory
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 lookup mode $pattern, add 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 of acquisition time of Chinese name files after 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 file, located in the root directory, principle: Access to the path is not a file, that is, the folder, so jump to the root path to do parsing.
Rewriteengine on
#一级目录法
#RewriteBase/
#二级目录法
rewritebase/listfile/
Rewritecond%{request_filename}!-f
Rewriterule (. *) Index.php?return=%{request_filename} [L]
*/
?>

JS file
Copy Code code as follows:

$ (document). Ready (function () {
Root node Delete return link
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 the return link is triggered at the root node, it is returned directly.
url = url.replace (Location.search, ""); If the link is carrying a return, truncate the return (generated by. htaccess)
url = url.substr (0,url.length-2); Start at 2nd after URL, avoid/#情况存在时跳转错误
url = url.substr (0,url.lastindexof ('/') +1); Truncate last folder, back level
window.location.href = URL;
return false; After processing, returns false to prevent the <a> tag from clicking on the jump.
});
if (Autoclickurl!= ') $ ("#back a"). Click ()
});

CSS file
Copy Code code as follows:

#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 file
Copy Code code as follows:

#原理: Access to the path is not a file, that is, a folder, so jump to the root path to do resolution get all the files in the current directory and list.
Rewriteengine on
#一级目录法
#RewriteBase/
#二级目录法
rewritebase/listfile/
Rewritecond%{request_filename}!-f
Rewriterule (. *) Index.php?return=%{request_filename} [L]

Core folder: listfile/images/
The effect chart is 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.