复杂的网站目录结构,想找个文件或模块,忘记在哪里了,或者陌生的目录结构,伤不起啊。。。
干脆为自己打造一个实用的小工具,对于需要研究discuz等优秀网站系统的cooder会有所帮助
-
- /*==================================================================*/
- /* 文件名:scandir.php */
- /* 概要: 目录文件遍历并生成文件树. */
- /* 作者: 翻身的咸鱼 */
- /* 创建时间:
2012-04-20 */
- /* 最后修改时间:2012-04-26 */
- /* copyright (c)2012 15053884@qq.com
*/
- /*==================================================================*/
- $startTime = microtime(true);
- ?>
-
- 目录文件遍历并生成文件树
-
-
- #form{
- width:650px;
- float:left;
- border:1px solid gray;
- padding:10px;
- margin:10px;
- font-size:16px;
- }
- #note{
- line-height:35px;
- }
- #exists{
- float:left;
- width:500px;
- border:1px solid gray;
- position:absolute;
- left:700px;
- top:73px;
- padding:10px;
- background:#cccccc;
- }
- #tree{
- border:1px solid gray;
- width:97%;
- float:left;
- padding:10px;
- margin:10px;
- }
- a{
- text-decoration:none;
- }
- input{
- font-size:16px;
- }
- .note{
- padding:10px;
- color:red;
- border:1px solid gray;
- background:#cccccc;
- }
- .message{
- padding:10px;
- border:1px solid gray;
- background:#cccccc;
- }
-
-
-
-
目录文件遍历并生成文件树-->目录文件,一目了然
-
-
已经存在的目录树文件:
-
- $txtfile = treeFileGet();
- $filenames = getTxtName($txtfile);
-
- for($i=0; $i
- $url = makeTxtUrl($filenames[$i]);
- $urls .= $url;
- }
- echo $urls;
- ?>
-
-
-
-
- if(trim($_POST["path"])!=""){
- $file_path =
rtrim(trim(str_replace("\\\\","/",$_POST["path"])),"/");
- if(is_dir($file_path)){
- $file_input = makeTxtName($file_path);
- $txtfile = treeFileGet();
- if(!in_array($file_input,getTxtName($txtfile))){
- @dirScan($file_path,$file_input);
- }else{
- echo "
注意:该目录已经扫描过了!!!!!!!
";
- }
- if($_POST["rescan"]=="true"){
- unlink($file_input);
- @dirScan($file_path,$file_input);
- }
- $dirs = pathGet($file_input);
- $file_num = getFileNum($dirs);
- $dir_num = getDirNum($dirs);
- $dir_path = makeDirPath($file_input);
- echo "
文件夹:".$dir_path." 文件夹数:".$dir_num." 文件数:".$file_num."
";
- makeDirTree($dirs);
- }else{
- $info = "输入的路径".$file_path."不是合法路径或目录树文件不存在,请检查后重新输入!";
- message($info);
- }
- }
- if(trim($_POST["dirfile"])!=""||$_GET["fn"]!=""){
- $file_path = trim($_POST["dirfile"]);
- $file_input = $_POST["dirfile"] ? $_POST["dirfile"] :
$_GET["fn"];
- $path =
str_replace("\\","/",dirname(__FILE__))."/".$file_input;
- if(is_file($path)){
- $dirs = pathGet($file_input);
- $file_num = getFileNum($dirs);
- $dir_num = getDirNum($dirs);
- $dir_path = makeDirPath($file_input);
- echo "
文件夹:".$dir_path." 文件夹数:".$dir_num." 文件数:".$file_num."
";
- makeDirTree($dirs);
- }else{
- $info = "输入的路径".$file_input."不是合法路径或目录树文件不存在,请检查后重新输入!";
- message($info);
- }
- }
- $stopTime = microtime(true);
- $timer = round(($stopTime-$startTime),4);
- echo "页面运行时间:".$timer."秒";
- /*递归方式,遍历目录至文件,并写入文件,每行为一个目录或文件*/
- function dirScan($file_path,$file_input){
- $handle = opendir($file_path);
- $i = 0;
- while($files=readdir($handle)){
- if($files != "." && $files != ".."){
- $paths = $file_path."/".$files;
- $hand = fopen($file_input,"a+");
- $in_write = $paths."\r\n";
- fwrite($hand,$in_write);
- dirScan($paths,$file_input);
- }
- }
- closedir($handle);
- }
- /*按行读取文件,返回以每行内容为值的数组,传入存储数据的文件名,返回$path_get二维数组*/
- function pathGet($file_input){
- $path_get = array();
- $handle = fopen($file_input,"r");
- while(!feof($handle)){
- $buffer = rtrim(fgets($handle,1024));
- array_push($path_get,$buffer);
- }
- return $path_get;
- fclose($handle);
- }
-
- /*获取目录,传入文件夹路径,返回$dir_name[]数组*/
- function dirGet($dir_path){
- if(is_dir($dir_path)){
- $dir_name = explode("/",$dir_path);
- }
- return $dir_name;
- }
- /*获取文件名*/
- function fileGet($file_path){
- if(is_file($file_path)){
- $file_name = explode("/",$file_path);
- }
- return $file_name;
- }
- /*生成目录树,传入路径数组,按照格式生成目录树*/
- function makeDirTree($dirs = array()){
- $location = strpos($dirs[0],"/")+1;
- $filename = substr($dirs[0],$location);
- $path = explode('/',$dirs[0]);
- for($j=0; $j
- $space = @str_repeat(' ',$j-2);
- $shu = @str_repeat('|',$j-2);
- $k = $j-1>1 ? 1 : $j-1;
- $shuheng = @str_repeat('|---',$k);
- $location = strrpos($path[$j],"/")+1;
- $filename = substr($path[$j],$location);
- echo $shu.$space.$shuheng.$path[$j-1]."
";
- }
- for($i=0; $i
- $space =
str_repeat(' ',count(explode('/',$dirs[$i]))-1);
- $location = strrpos($dirs[$i],"/")+1;
- $filename = substr($dirs[$i],$location);
- $filesize = round(@filesize($dirs[$i])/pow(1024,1),2);
- if(is_file($dirs[$i])){
- echo "|".$space."|---".$filename."(".$filesize."KB)
";
- }else{
- echo "|".$space."|---".$filename."
";
- }
- }
- }
- /*生成文件名*/
- function makeTxtName($file_path){
- $vowels = array("/",":");
- $filename = str_replace($vowels,"-",$file_path).".txt";
- return $filename;
- }
- /*获取已经存在的目录树文件*/
- function treeFileGet(){
- $file_path = str_replace("\\","/",dirname(__FILE__));
- $exists = array();
- $handle = opendir($file_path);
- while($files=readdir($handle)){
- $paths = $file_path."/".$files;
- $location = strrpos($files,".")+1;
- $exname = substr($files,$location);
- if(is_file($paths) && $exname=="txt"){
- array_push($exists,$paths);
- }
- }
- return $exists;
- closedir($handle);
- }
- /*获取TXT文件名*/
- function getTxtName($txtfile){
- $filenames = array();
- for($i=0; $i
- $location = strrpos($txtfile[$i],"/")+1;
- $filename = substr($txtfile[$i],$location);
- array_push($filenames,$filename);
- }
- return $filenames;
- }
- /*生成超链接*/
- function makeTxtUrl($filename){
- $filesize = round(filesize($filename)/pow(1024,1),2);
- $url = "".$filename."(".$filesize."KB)
";
- return $url;
- }
- /*获取文件数*/
- function getFileNum($dirs){
- $file_num = 0;
- for($i=0; $i
- if(is_file($dirs[$i])){
- $file_num++;
- }
- }
- return $file_num;
- }
- /*获取文件夹数*/
- function getDirNum($dirs){
- $dir_num = 0;
- for($i=0; $i
- if(is_dir($dirs[$i])){
- $dir_num++;
- }
- }
- return $dir_num;
- }
- /*生成目录路径*/
- function makeDirPath($dir_path){
- $dirpath =
str_replace("-","/",str_replace("--",":/",$dir_path));
- $dirpath = explode(".txt",$dirpath);
- return $dirpath["0"]."/";
- }
- /*生成NOTE框*/
- function message($info){
- echo "
".$info."
";
- }
-
- ?>
-
-
-
- /*==================================================================*/
- /* 文件名:highlight.php */
- /* 概要: 代码加亮.
*/
- /* 作者: 翻身的咸鱼 */
- /* 创建时间:
2012-04-21 */
- /* 最后修改时间:2012-04-26 */
- /* copyright (c)2012 15053884@qq.com
*/
- /*==================================================================*/
- header("Content-Type:text/html;charset=gb2312");
- ini_set("memory_limit","100M");
- $filename = $_GET["fn"];
- $file_contents = file_get_contents($filename);
- echo "
".$filename."文件源代码
";
- echo iconv('utf-8', 'gb2312//IGNORE',
prasePHPCode($file_contents));//编码转换
- function prasePHPCode($source_code){
- if (is_array($source_code)){
- return false;
- }
- $source_code = explode("\n", str_replace(array("\r\n", "\r"), "\n",
$source_code));
- $line_count = 1;
- foreach ($source_code as $code_line){
- $formatted_code .= '
'.$line_count.' | ';
- $line_count++;
- if (ereg('<\?(php)?[^[:graph:]]', $code_line)){
- $formatted_code .= '
'.
str_replace(array('', ' '), '',
highlight_string($code_line, true)).' |
';
- }else{
- $formatted_code .= '
'.ereg_replace('(<\?php )+', '',
str_replace(array('', ' '), '',
highlight_string('';
- }
- }
- return '';
- }
- ?>
原文地址:
阅读(307) | 评论(0) | 转发(0) |
|