Single index.php for PHP arbitrary-level folder traversal (zjmainstay original) _php tutorial

Source: Internet
Author: User
Tags file copy glob
The following are the core files:
index.php file
Copy CodeThe code is as follows:
Header (' content-type:text/html charset:utf-8 ');
Date_default_timezone_set (' PRC ');
$rootDir = ' listfile '; Site root, load all files of this program
Site Base_url Setup Method:
For versatility, it is now default to use method two, and note that modifying a method is also necessary to modify the. htaccess file
Method One: Set the Site Directory as the root directory
corresponding. htaccess:
#RewriteBase/
$base _url = ' http://www.listfile.com/';
Method Two: Set the site subdirectory as the root directory
corresponding. htaccess:
rewritebase/listfile/
$base _url = ' http://www.test.com/'. $rootDir. ' /';
Resolve 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 = ' * '; ' * ' Search all files, can intelligently match, such as *.jpg search jpg file, *. {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);
?>



<title>List Files</title>








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 image file ',
' gif ' = ' gif picture file ',
' png ' = ' png image file ',
' MP3 ' = ' mp3 file ',
' Zip ' = ' zip archive ',
' rar ' = ' rar archive 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 all files in the current folder by law
echo "..";
echo "";
echo "Name Size";
echo "Type modification Date";
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 "{$file}";
echo "{$files [' filesize '] [$index]} {$filetype}";
echo "{$files [' filemtime '] [$index]}";
}
Echo ';
?>


Folder Traversal function
function Scandir_through ($dir, $pattern = ' * ', $skip =false, $subInclude =true, $flag =glob_brace) {
$files = Array ();
Get all files and folders in the current directory
$items = Glob ($dir. '/*');
Traverse all items, and if set $subinclude to True, continue traversing subdirectories
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, it is added 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 in order to solve the problem of obtaining time of Chinese name file 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, the principle: Access path non-file, that is, folder, so jump to the root path to do the parsing.
Rewriteengine on
#一级目录法
#RewriteBase/
#二级目录法
rewritebase/listfile/
Rewritecond%{request_filename}!-f
Rewriterule (. *) Index.php?return=%{request_filename} [L]
*/
?>

JS file
Copy CodeThe code is 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 carries a return, truncate the return (generated by. htaccess)
url = url.substr (0,url.length-2); Avoid/#情况存在时跳转错误 starting at 2nd bit after URL
url = url.substr (0,url.lastindexof ('/') +1); Truncate the last level of folder, back to level one
window.location.href = URL;
return false; After processing, return false to prevent tabs from clicking after the jump.
});
if (autoclickurl! = ") $ (" #back a "). Click ()
});

CSS file
Copy CodeThe code is 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 CodeThe code is as follows:
#原理: Access path is not a file, that is, a folder, so jump to the root path to parse to 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/
As follows:
ListFile


Package Download: Download

http://www.bkjia.com/PHPjc/325787.html www.bkjia.com true http://www.bkjia.com/PHPjc/325787.html techarticle The following is the core file: index.php file copy code code as follows:? PHP header (' content-type:text/html charset:utf-8 '); Date_default_timezone_set (' PRC '); $rootDir = ' listfile '; Station ...

  • 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.