分享一个非常实用的代码查看工具

来源:互联网 发布:linux是典型的应用软件 编辑:程序博客网 时间:2024/05/27 01:48
复杂的网站目录结构,想找个文件或模块,忘记在哪里了,或者陌生的目录结构,伤不起啊。。。
干脆为自己打造一个实用的小工具,对于需要研究discuz等优秀网站系统的cooder会有所帮助
  1. <?php
  2. /*==================================================================*/
  3. /* 文件名:scandir.php */
  4. /* 概要: 目录文件遍历并生成文件树. */
  5. /* 作者: 翻身的咸鱼 */
  6. /* 创建时间: 2012-04-20 */
  7. /* 最后修改时间:2012-04-26 */
  8. /* copyright (c)2012 15053884@qq.com */
  9. /*==================================================================*/
  10. $startTime = microtime(true);
  11. ?>
  12. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  13. <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN" style="zoom: 100%; ">
  14. <head>
  15. <title>目录文件遍历并生成文件树</title>
  16. <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
  17. <style>
  18. #form{
  19. width:650px;
  20. float:left;
  21. border:1px solid gray;
  22. padding:10px;
  23. margin:10px;
  24. font-size:16px;
  25. }
  26. #note{
  27. line-height:35px;
  28. }
  29. #exists{
  30. float:left;
  31. width:500px;
  32. border:1px solid gray;
  33. position:absolute;
  34. left:700px;
  35. top:73px;
  36. padding:10px;
  37. background:#cccccc;
  38. }
  39. #tree{
  40. border:1px solid gray;
  41. width:97%;
  42. float:left;
  43. padding:10px;
  44. margin:10px;
  45. }
  46. a{
  47. text-decoration:none;
  48. }
  49. input{
  50. font-size:16px;
  51. }
  52. .note{
  53. padding:10px;
  54. color:red;
  55. border:1px solid gray;
  56. background:#cccccc;
  57. }
  58. .message{
  59. padding:10px;
  60. border:1px solid gray;
  61. background:#cccccc;
  62. }
  63. </style>
  64. </head>
  65. <body bgcolor="#c0dcc0">
  66. <h3><center>目录文件遍历并生成文件树-->目录文件,一目了然</center></h3>
  67. <div id="form">
  68. <form action="" method="POST">
  69. <div style="height:35px;line-height:35px;">
  70. 输入文件夹路径: <input type="text" name="path" value="" size="50">
  71. <input type="hidden" name="rescan" value="false">
  • <div id="note">
  • 格式范例:D:/root/discuz 或 D:\root\discuz<br />[注意:不要挑战文件超过<font color="red">3万个</font>的超级目录。否则...]
  • <hr />
  • <div style="height:35px;line-height:35px;">
  • 输入txt目录文件名:<input type="text" name="dirfile" value="" size="50">
  • <div id="note">
  • 格式范例:D:/root/scandir/D--root-discuz.txt
  • <div style="width:100%;text-align:right">
  • <input type="submit" name="submit" value="创建并显示文件树">
  • <a href="http://192.168.60.7/scandir/scandir.rar">下载此工具
  • </form>
  • <div id="exists">已经存在的目录树文件:<br />
  • <?php
  • $txtfile = treeFileGet();
  • $filenames = getTxtName($txtfile);
  • for($i=0; $i<count($filenames); $i++){
  • $url = makeTxtUrl($filenames[$i]);
  • $urls .= $url;
  • }
  • echo $urls;
  • ?>
  • <div id="tree">
  • <?php
  • 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 "<div class='note'>注意:该目录已经扫描过了!!!!!!!<form action='' method='POST'><input type='hidden' name='path' value=".$file_path."><input type='submit' name='submit' value='重新扫描'></form>";
  • }
  • 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 "<div class='message'>文件夹:<font color='red'>".$dir_path."</font> 文件夹数:<font color='red'>".$dir_num."</font> 文件数:<font color='red'>".$file_num."</font>";
  • makeDirTree($dirs);
  • }else{
  • $info = "输入的路径<font color='red'>".$file_path."</font>不是合法路径或目录树文件不存在,请检查后重新输入!";
  • 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 "<div class='message'>文件夹:<font color='red'>".$dir_path."</font> 文件夹数:<font color='red'>".$dir_num."</font> 文件数:<font color='red'>".$file_num."</font>";
  • makeDirTree($dirs);
  • }else{
  • $info = "输入的路径<font color='red'>".$file_input."</font>不是合法路径或目录树文件不存在,请检查后重新输入!";
  • 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<count($path); $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]."<br />";
  • }
  • for($i=0; $i<count($dirs); $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."|---<a href='highlight.php?fn=".$dirs[$i]."' title='点击查看源文件' target='_blank'>".$filename."(".$filesize."KB)<br />";
  • }else{
  • echo "|".$space."|---".$filename."<br />";
  • }
  • }
  • }
  • /*生成文件名*/
  • 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<count($txtfile); $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 = "<a href='scandir.php?fn=".$filename."' >".$filename."(".$filesize."KB)<br />";
  • return $url;
  • }
  • /*获取文件数*/
  • function getFileNum($dirs){
  • $file_num = 0;
  • for($i=0; $i<count($dirs); $i++){
  • if(is_file($dirs[$i])){
  • $file_num++;
  • }
  • }
  • return $file_num;
  • }
  • /*获取文件夹数*/
  • function getDirNum($dirs){
  • $dir_num = 0;
  • for($i=0; $i<count($dirs); $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 "<div class='message'>".$info."";
  • }
  • ?>
  • </body>
  • </html>

  • 复制代码
    1. <?php
    2. /*==================================================================*/
    3. /* 文件名:highlight.php */
    4. /* 概要: 代码加亮. */
    5. /* 作者: 翻身的咸鱼 */
    6. /* 创建时间: 2012-04-21 */
    7. /* 最后修改时间:2012-04-26 */
    8. /* copyright (c)2012 15053884@qq.com */
    9. /*==================================================================*/
    10. header("Content-Type:text/html;charset=gb2312");
    11. ini_set("memory_limit","100M");
    12. $filename = $_GET["fn"];
    13. $file_contents = file_get_contents($filename);
    14. echo "<h1>".$filename."文件源代码</h1>";
    15. echo iconv('utf-8', 'gb2312//IGNORE', prasePHPCode($file_contents));//编码转换
    16. function prasePHPCode($source_code){
    17. if (is_array($source_code)){
    18. return false;
    19. }
    20. $source_code = explode("\n", str_replace(array("\r\n", "\r"), "\n", $source_code));
    21. $line_count = 1;
    22. foreach ($source_code as $code_line){
    23. $formatted_code .= '<tr><td bgcolor="#efede8">'.$line_count.'</td>';
    24. $line_count++;
    25. if (ereg('<\?(php)?[^[:graph:]]', $code_line)){
    26. $formatted_code .= '<td bgcolor="#efede8">'. str_replace(array('<code>', '</code>'), '', highlight_string($code_line, true)).'</td></tr>';
    27. }else{
    28. $formatted_code .= '<td bgcolor="#efede8">'.ereg_replace('(<\?php )+', '', str_replace(array('<code>', '</code>'), '', highlight_string('<?php '.$code_line, true))).'</td></tr>';
    29. }
    30. }
    31. return '<table style="width:800px;font: 1em Consolas, \'andale mono\', \'monotype.com\', \'lucida console\', monospace;">'.$formatted_code.'</table>';
    32. }
    33. ?>

    原文地址:http://bbs.lampbrother.net/read-htm-tid-118290.html

    阅读(55) | 评论(0) | 转发(0) |
    0

    上一篇:战地日记PartI

    下一篇:让我们sy现实,让我们yy理想---谨以此文献给我最最可爱的奋斗在人生路上的兄弟们

    相关热门文章
    给主人留下些什么吧!~~
    原创粉丝点击