1. //$path 指定的目录,默认为当前目录
  2. //$ifchild 是否显示子目录文件列表,默认不显示
  3. //$curpath 显示当前的路径,默认为从当前目录开始;这个主要是为了显示确定href路径
  4. function openpath($path=".",$ifchild=false,$curpath=".")
  5. {
  6. $handle = opendir($path);
  7. if($handle)
  8. {
  9. while(false !== ($file = readdir($handle)))
  10. {
  11. if ($file != "." && $file != "..")
  12. {
  13. $fullPath = $path.DIRECTORY_SEPARATOR.$file;
  14. if(is_dir($fullPath))//如果是目录文件
  15. {
  16. if($ifchild)//如果设置了显示子目录
  17. {
  18. //递归
  19. openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,$curpath.DIRECTORY_SEPARATOR.$file);
  20. }
  21. else
  22. {
  23. echo "
  24. \n";
  25. }
  26. }
  27. else if($file != basename(__FILE__))//排除当前执行脚本
  28. {
  29. echo "
  30. \n";
  31. }
  32. else
  33. {
  34. echo $file;
  35. }
  36. }
  37. }
  38. }
  39. closedir($handle);
  40. }
复制代码

因为要提供路径选择的功能,要是有一个下拉菜单,里面有待选路径的显示就好了。

2、取得当前文件下所有子文件路径的代码:

  1. *$path 指定的目录,默认为当前目录
  2. *$ifchild 是否获取子目录文件列表,默认不获取
  3. *$curpath 显示当前的路径,默认为从当前目录开始
  4. *&$pach_html_srt 传递一个外部变量的引用进来,因为此方法有可能被递归调用,所以以这样的方式来保存
  5. * 一些信息,也可以用全局变量来实现,在函数内部变量改变也影响到外部。
  6. *&$path_ref_count 原理同上,一个计数标志,如果递归,计数器从上一次保存的值开始自增
  7. */
  8. function openpath($path=".",$ifchild=false,&$path_html_str,&$path_ref_count)
  9. {
  10. $handle = opendir($path);
  11. if($handle)
  12. {
  13. while(false !== ($file = readdir($handle)))
  14. {
  15. if ($file != "." && $file != "..")
  16. {
  17. $fullPath = $path.DIRECTORY_SEPARATOR.$file;
  18. if(is_dir($fullPath))//如果文件是目录
  19. {
  20. $path_html_str.='
  21. ';
  22. $path_html_str.=$file.'
      ';
    • if($ifchild)
    • {
    • //递归
    • openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,&$path_html_str,&$path_ref_count);
    • }
    • $path_html_str.='
  23. ';
  24. }
  25. }
  26. }
  27. }
  28. closedir($handle);
  29. }
复制代码

有了上面的方法,就可以在前台用jquery mcDropdown插件来让用户可以通过下拉菜单选择想进入的目录,所以需要封装成指定格式:

  1. $path_html_str ='';
  2. openpath(".",true,&$path_html_str,&$path_ref_count);
  3. $path_html_str = '
      '.$path_html_str.'
    ';
  4. $path_html_str = str_replace ( "
      ", '', $path_html_str );
    • ?>
    复制代码

    这样把$path_html_str传到前台,显示出来就是一个符合mcDropdown要求的无序列表,就可以显示相应的待选列表了。

    完整代码如下:1、test.html

    复制代码

    2、test.

    1. $path_ref_count = 1;
    2. $path_html_str ='';
    3. openpath(".",true,&$path_html_str,&$path_ref_count);
    4. $path_html_str = '
        '.$path_html_str.'
      ';
    5. $path_html_str = str_replace ( "
        ", '', $path_html_str );
      • //var_dump($path_info);
      • //var_dump($path_html_str);
      • $str_buffer = file_get_contents (dirname(__FILE__).DIRECTORY_SEPARATOR.'test.html');
      • $str_buffer = str_replace ( "#categorymenu#", $path_html_str, $str_buffer );
      • $str_buffer = str_replace ( "#delim#", DIRECTORY_SEPARATOR, $str_buffer );
      • echo $str_buffer;
      • /*获取指定目录文件路径列表
      • *$path 指定的目录,默认为当前目录
      • *$ifchild 是否获取子目录文件列表,默认不获取
      • *$curpath 显示当前的路径,默认为从当前目录开始
      • *&$pach_html_srt 传递一个外部变量的引用进来,因为此方法有可能被递归调用,所以以这样的方式来保存
      • * 一些信息,也可以用全局变量来实现,在函数内部变量改变也影响到外部。
      • *&$path_ref_count 原理同上,一个计数标志,如果递归,计数器从上一次保存的值开始自增
      • */
      • function openpath($path=".",$ifchild=false,&$path_html_str,&$path_ref_count)
      • {
      • $handle = opendir($path);
      • if($handle)
      • {
      • while(false !== ($file = readdir($handle)))
      • {
      • if ($file != "." && $file != "..")
      • {
      • $fullPath = $path.DIRECTORY_SEPARATOR.$file;
      • if(is_dir($fullPath))//如果文件是目录
      • {
      • $path_html_str.='
      • ';
      • $path_html_str.=$file.'
          ';
        • if($ifchild)
        • {
        • //递归
        • openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,&$path_html_str,&$path_ref_count);
        • }
        • $path_html_str.='
      • ';
      • }
      • }
      • }
      • }
      • closedir($handle);
      • }
      • ?>
      复制代码

      jquery mcDropdown 插件下载地址:http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm。



      09-14 12:16