Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1552808
  • 博文数量: 1481
  • 博客积分: 26784
  • 博客等级: 上将
  • 技术积分: 17045
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-12 09:22
文章分类

全部博文(1481)

文章存档

2014年(10)

2013年(353)

2012年(700)

2011年(418)

分类: 系统运维

2012-05-02 10:26:50

复杂的网站目录结构,想找个文件或模块,忘记在哪里了,或者陌生的目录结构,伤不起啊。。。
干脆为自己打造一个实用的小工具,对于需要研究discuz等优秀网站系统的cooder会有所帮助
  1. /*==================================================================*/
  2. /* 文件名:scandir.php */
  3. /* 概要: 目录文件遍历并生成文件树. */
  4. /* 作者: 翻身的咸鱼 */
  5. /* 创建时间: 2012-04-20 */
  6. /* 最后修改时间:2012-04-26 */
  7. /* copyright (c)2012 15053884@qq.com */
  8. /*==================================================================*/
  9. $startTime = microtime(true);
  10. ?>
  11. 目录文件遍历并生成文件树
  12. 目录文件遍历并生成文件树-->目录文件,一目了然

  13. 输入文件夹路径:
  • 格式范例:D:/root/discuz 或 D:\root\discuz
    [注意:不要挑战文件超过3万个的超级目录。否则...]

  • 输入txt目录文件名:
  • 格式范例:D:/root/scandir/D--root-discuz.txt
  • 下载此工具
  • 已经存在的目录树文件:
  • $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."
    ";
  • }
  • ?>

    1. /*==================================================================*/
    2. /* 文件名:highlight.php */
    3. /* 概要: 代码加亮. */
    4. /* 作者: 翻身的咸鱼 */
    5. /* 创建时间: 2012-04-21 */
    6. /* 最后修改时间:2012-04-26 */
    7. /* copyright (c)2012 15053884@qq.com */
    8. /*==================================================================*/
    9. header("Content-Type:text/html;charset=gb2312");
    10. ini_set("memory_limit","100M");
    11. $filename = $_GET["fn"];
    12. $file_contents = file_get_contents($filename);
    13. echo "

      ".$filename."文件源代码

      ";
    14. echo iconv('utf-8', 'gb2312//IGNORE', prasePHPCode($file_contents));//编码转换
    15. function prasePHPCode($source_code){
    16. if (is_array($source_code)){
    17. return false;
    18. }
    19. $source_code = explode("\n", str_replace(array("\r\n", "\r"), "\n", $source_code));
    20. $line_count = 1;
    21. foreach ($source_code as $code_line){
    22. $formatted_code .= ''.$line_count.'';
    23. $line_count++;
    24. if (ereg('<\?(php)?[^[:graph:]]', $code_line)){
    25. $formatted_code .= ''. str_replace(array('', ''), '', highlight_string($code_line, true)).'';
    26. }else{
    27. $formatted_code .= ''.ereg_replace('(<\?php )+', '', str_replace(array('', ''), '', highlight_string('';
    28. }
    29. }
    30. return ''.$formatted_code.'
      ';
    31. }
    32. ?>

    原文地址:

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

    上一篇:战地日记PartI

    下一篇:【捷哥浅谈PHP】第六弹 ---- 使用for循环输出九九乘法表

    给主人留下些什么吧!~~