登录  /  注册
php mcDropdown实现文件路径可在下拉框选择的方法
php中文网
发布: 2016-07-25 09:00:05
原创
1067人浏览过
  1. //获取指定目录下的文件列表
  2. //$path 指定的目录,默认为当前目录
  3. //$ifchild 是否显示子目录文件列表,默认不显示
  4. //$curpath 显示当前的路径,默认为从当前目录开始;这个主要是为了显示确定href路径
  5. function openpath($path=".",$ifchild=false,$curpath=".")
  6. {
  7. $handle = opendir($path);
  8. if($handle)
  9. {
  10. while(false !== ($file = readdir($handle)))
  11. {
  12. if ($file != "." && $file != "..")
  13. {
  14. $fullPath = $path.DIRECTORY_SEPARATOR.$file;
  15. if(is_dir($fullPath))//如果是目录文件
  16. {
  17. if($ifchild)//如果设置了显示子目录
  18. {
  19. //递归
  20. openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,$curpath.DIRECTORY_SEPARATOR.$file);
  21. }
  22. else
  23. {
  24. echo "
  25. $file
  26. \n";
  27. }
  28. }
  29. else if($file != basename(__FILE__))//排除当前执行脚本
  30. {
  31. echo "
  32. $file
  33. \n";
  34. }
  35. else
  36. {
  37. echo $file;
  38. }
  39. }
  40. }
  41. }
  42. closedir($handle);
  43. }
复制代码

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

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

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

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

  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 );
    • ?>
    复制代码

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

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

    1. jquery mcDropdown实现文件路径可在下拉框选择的方法_bbs.it-home.org
    2. Please select a category:
    3. #categorymenu#
    复制代码

    2、test.php

    1. //目录信息处理
    2. $path_ref_count = 1;
    3. $path_html_str ='';
    4. openpath(".",true,&$path_html_str,&$path_ref_count);
    5. $path_html_str = '
        '.$path_html_str.'
      ';
    6. $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。



      来源:php中文网
      本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
      热门推荐
      热门教程
      更多>
      最新下载
      更多>
      网站特效
      网站源码
      网站素材
      前端模板
      关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
      php中文网:公益在线php培训,帮助PHP学习者快速成长!
      关注服务号 技术交流群
      PHP中文网订阅号
      每天精选资源文章推送
      PHP中文网APP
      随时随地碎片化学习
      PHP中文网抖音号
      发现有趣的

      Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

       | 本站CDN由 数掘科技 提供

      登录PHP中文网,和优秀的人一起学习!
      全站2000+教程免费学