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

复杂的网站目录结构,想找个文件或模块,忘记在哪里了,或者陌生的目录结构,伤不起啊。。。
干脆为自己打造一个实用的小工具,对于需要研究discuz等优秀网站系统的cooder会有所帮助
  1. <?php
  2. /*==================================================================*/
  3. /* 文件名:scandir.php */
  4. /* 概要: 目录文件遍历并生成文件树. */
  5. /* 作者: 翻身的咸鱼 */
  6. /* 创建时间: 2012-04-20 */
  7. /* 最后修改时间:2012-04-26 */
  8. /* copyright (c)2012 [email protected] */
  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">
  72. </div>
  73. <div id="note">
  74. 格式范例:D:/root/discuz 或 D:\root\discuz<br />[注意:不要挑战文件超过<font color="red">3万个</font>的超级目录。否则...]
  75. </div>
  76. <hr />
  77. <div style="height:35px;line-height:35px;">
  78. 输入txt目录文件名:<input type="text" name="dirfile" value="" size="50">
  79. </div>
  80. <div id="note">
  81. 格式范例:D:/root/scandir/D--root-discuz.txt
  82. </div>
  83. <div style="width:100%;text-align:right">
  84. <input type="submit" name="submit" value="创建并显示文件树">
  85. <a href="http://192.168.60.7/scandir/scandir.rar">下载此工具</a>
  86. </div>
  87. </form>
  88. </div>
  89. <div id="exists">已经存在的目录树文件:<br />
  90. <?php
  91. $txtfile = treeFileGet();
  92. $filenames = getTxtName($txtfile);
  93. for($i=0; $i<count($filenames); $i++){
  94. $url = makeTxtUrl($filenames[$i]);
  95. $urls .= $url;
  96. }
  97. echo $urls;
  98. ?>
  99. </div>
  100. <div id="tree">
  101. <?php
  102. if(trim($_POST["path"])!=""){
  103. $file_path = rtrim(trim(str_replace("\\\\","/",$_POST["path"])),"/");
  104. if(is_dir($file_path)){
  105. $file_input = makeTxtName($file_path);
  106. $txtfile = treeFileGet();
  107. if(!in_array($file_input,getTxtName($txtfile))){
  108. @dirScan($file_path,$file_input);
  109. }else{
  110. echo "<div class='note'>注意:该目录已经扫描过了!!!!!!!<form action='' method='POST'><input type='hidden' name='path' value=".$file_path."><input type='submit' name='submit' value='重新扫描'></form></div>";
  111. }
  112. if($_POST["rescan"]=="true"){
  113. unlink($file_input);
  114. @dirScan($file_path,$file_input);
  115. }
  116. $dirs = pathGet($file_input);
  117. $file_num = getFileNum($dirs);
  118. $dir_num = getDirNum($dirs);
  119. $dir_path = makeDirPath($file_input);
  120. echo "<div class='message'>文件夹:<font color='red'>".$dir_path."</font> 文件夹数:<font color='red'>".$dir_num."</font> 文件数:<font color='red'>".$file_num."</font></div>";
  121. makeDirTree($dirs);
  122. }else{
  123. $info = "输入的路径<font color='red'>".$file_path."</font>不是合法路径或目录树文件不存在,请检查后重新输入!";
  124. message($info);
  125. }
  126. }
  127. if(trim($_POST["dirfile"])!=""||$_GET["fn"]!=""){
  128. $file_path = trim($_POST["dirfile"]);
  129. $file_input = $_POST["dirfile"] ? $_POST["dirfile"] : $_GET["fn"];
  130. $path = str_replace("\\","/",dirname(__FILE__))."/".$file_input;
  131. if(is_file($path)){
  132. $dirs = pathGet($file_input);
  133. $file_num = getFileNum($dirs);
  134. $dir_num = getDirNum($dirs);
  135. $dir_path = makeDirPath($file_input);
  136. echo "<div class='message'>文件夹:<font color='red'>".$dir_path."</font> 文件夹数:<font color='red'>".$dir_num."</font> 文件数:<font color='red'>".$file_num."</font></div>";
  137. makeDirTree($dirs);
  138. }else{
  139. $info = "输入的路径<font color='red'>".$file_input."</font>不是合法路径或目录树文件不存在,请检查后重新输入!";
  140. message($info);
  141. }
  142. }
  143. $stopTime = microtime(true);
  144. $timer = round(($stopTime-$startTime),4);
  145. echo "页面运行时间:".$timer."秒";
  146. /*递归方式,遍历目录至文件,并写入文件,每行为一个目录或文件*/
  147. function dirScan($file_path,$file_input){
  148. $handle = opendir($file_path);
  149. $i = 0;
  150. while($files=readdir($handle)){
  151. if($files != "." && $files != ".."){
  152. $paths = $file_path."/".$files;
  153. $hand = fopen($file_input,"a+");
  154. $in_write = $paths."\r\n";
  155. fwrite($hand,$in_write);
  156. dirScan($paths,$file_input);
  157. }
  158. }
  159. closedir($handle);
  160. }
  161. /*按行读取文件,返回以每行内容为值的数组,传入存储数据的文件名,返回$path_get二维数组*/
  162. function pathGet($file_input){
  163. $path_get = array();
  164. $handle = fopen($file_input,"r");
  165. while(!feof($handle)){
  166. $buffer = rtrim(fgets($handle,1024));
  167. array_push($path_get,$buffer);
  168. }
  169. return $path_get;
  170. fclose($handle);
  171. }
  172. /*获取目录,传入文件夹路径,返回$dir_name[]数组*/
  173. function dirGet($dir_path){
  174. if(is_dir($dir_path)){
  175. $dir_name = explode("/",$dir_path);
  176. }
  177. return $dir_name;
  178. }
  179. /*获取文件名*/
  180. function fileGet($file_path){
  181. if(is_file($file_path)){
  182. $file_name = explode("/",$file_path);
  183. }
  184. return $file_name;
  185. }
  186. /*生成目录树,传入路径数组,按照格式生成目录树*/
  187. function makeDirTree($dirs = array()){
  188. $location = strpos($dirs[0],"/")+1;
  189. $filename = substr($dirs[0],$location);
  190. $path = explode('/',$dirs[0]);
  191. for($j=0; $j<count($path); $j++){
  192. $space = @str_repeat(' ',$j-2);
  193. $shu = @str_repeat('|',$j-2);
  194. $k = $j-1>1 ? 1 : $j-1;
  195. $shuheng = @str_repeat('|---',$k);
  196. $location = strrpos($path[$j],"/")+1;
  197. $filename = substr($path[$j],$location);
  198. echo $shu.$space.$shuheng.$path[$j-1]."<br />";
  199. }
  200. for($i=0; $i<count($dirs); $i++){
  201. $space = str_repeat(' ',count(explode('/',$dirs[$i]))-1);
  202. $location = strrpos($dirs[$i],"/")+1;
  203. $filename = substr($dirs[$i],$location);
  204. $filesize = round(@filesize($dirs[$i])/pow(1024,1),2);
  205. if(is_file($dirs[$i])){
  206. echo "|".$space."|---<a href='highlight.php?fn=".$dirs[$i]."' title='点击查看源文件' target='_blank'>".$filename."</a>(".$filesize."KB)<br />";
  207. }else{
  208. echo "|".$space."|---".$filename."<br />";
  209. }
  210. }
  211. }
  212. /*生成文件名*/
  213. function makeTxtName($file_path){
  214. $vowels = array("/",":");
  215. $filename = str_replace($vowels,"-",$file_path).".txt";
  216. return $filename;
  217. }
  218. /*获取已经存在的目录树文件*/
  219. function treeFileGet(){
  220. $file_path = str_replace("\\","/",dirname(__FILE__));
  221. $exists = array();
  222. $handle = opendir($file_path);
  223. while($files=readdir($handle)){
  224. $paths = $file_path."/".$files;
  225. $location = strrpos($files,".")+1;
  226. $exname = substr($files,$location);
  227. if(is_file($paths) && $exname=="txt"){
  228. array_push($exists,$paths);
  229. }
  230. }
  231. return $exists;
  232. closedir($handle);
  233. }
  234. /*获取TXT文件名*/
  235. function getTxtName($txtfile){
  236. $filenames = array();
  237. for($i=0; $i<count($txtfile); $i++){
  238. $location = strrpos($txtfile[$i],"/")+1;
  239. $filename = substr($txtfile[$i],$location);
  240. array_push($filenames,$filename);
  241. }
  242. return $filenames;
  243. }
  244. /*生成超链接*/
  245. function makeTxtUrl($filename){
  246. $filesize = round(filesize($filename)/pow(1024,1),2);
  247. $url = "<a href='scandir.php?fn=".$filename."' >".$filename."</a>(".$filesize."KB)<br />";
  248. return $url;
  249. }
  250. /*获取文件数*/
  251. function getFileNum($dirs){
  252. $file_num = 0;
  253. for($i=0; $i<count($dirs); $i++){
  254. if(is_file($dirs[$i])){
  255. $file_num++;
  256. }
  257. }
  258. return $file_num;
  259. }
  260. /*获取文件夹数*/
  261. function getDirNum($dirs){
  262. $dir_num = 0;
  263. for($i=0; $i<count($dirs); $i++){
  264. if(is_dir($dirs[$i])){
  265. $dir_num++;
  266. }
  267. }
  268. return $dir_num;
  269. }
  270. /*生成目录路径*/
  271. function makeDirPath($dir_path){
  272. $dirpath = str_replace("-","/",str_replace("--",":/",$dir_path));
  273. $dirpath = explode(".txt",$dirpath);
  274. return $dirpath["0"]."/";
  275. }
  276. /*生成NOTE框*/
  277. function message($info){
  278. echo "<div class='message'>".$info."</div>";
  279. }
  280. ?>
  281. </div>
  282. </body>
  283. </html>

  1. <?php
  2. /*==================================================================*/
  3. /* 文件名:highlight.php */
  4. /* 概要: 代码加亮. */
  5. /* 作者: 翻身的咸鱼 */
  6. /* 创建时间: 2012-04-21 */
  7. /* 最后修改时间:2012-04-26 */
  8. /* copyright (c)2012 [email protected] */
  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. ?>

你可能感兴趣的:(代码)