利用php+mcDropdown实现文件路径可在下拉框选择
黄金路中文字幕,自考365网站,第四届cctv相声大赛
最近用php进行文件目录信息读取,在网上弄到下面一段代码:
//获取指定目录下的文件列表
//$path 指定的目录,默认为当前目录
//$ifchild 是否显示子目录文件列表,默认不显示
//$curpath 显示当前的路径,默认为从当前目录开始;这个主要是为了显示确定href路径
function openpath($path=".",$ifchild=false,$curpath=".")
{
$handle = opendir($path);
if($handle)
{
while(false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$fullpath = $path.directory_separator.$file;
if(is_dir($fullpath))//如果是目录文件
{
if($ifchild)//如果设置了显示子目录
{
//递归
openpath($path.directory_separator.$file,$ifchild,$curpath.directory_separator.$file);
}
else
{
echo "<li><a href=\"$curpath/$file \" target=\"_blank\">$file</a></li>\n";
}
}
else if($file != basename(__file__))//排除当前执行脚本
{
echo "<li><a href=\"$curpath/$file \" target=\"_blank\">$file</a></li>\n";
}
else
{
echo $file;
}
}
}
}
closedir($handle);
}
因为自己要提供路径选择的功能,最好是有一个下拉菜单,里面有待选路径的显示,这样方便很多,于是自己改了改,把路径输出成一个无序列表。
下面是一个取得当前文件下所有子文件路径的代码:
/*获取指定目录文件路径列表
*$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.='<li rel="'.$path_ref_count++.'">';
$path_html_str.=$file.'<ul>';
if($ifchild)
{
//递归
openpath($path.directory_separator.$file,$ifchild,&$path_html_str,&$path_ref_count);
}
$path_html_str.='</ul></li>';
}
}
}
}
closedir($handle);
}
有了上面的方法,我就可以在前台用jquery mcdropdown插件来让用户可以通过下拉菜单选择想进入的目录,所以需要封装成指定格式:
$path_ref_count = 1;
$path_html_str ='';
openpath(".",true,&$path_html_str,&$path_ref_count);
$path_html_str = '<ul id="categorymenu" class="mcdropdown_menu">'.$path_html_str.'</ul>';
$path_html_str = str_replace ( "<ul></ul>", '', $path_html_str );
这样我把$path_html_str传到前台,显示出来的就是一个符合mcdropdown要求的无序列表,就可以显示相应的待选列表了。
完整代码如下:
test.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>test</title>
<script type="text/javascript" src="./lib/jquery.js"></script>
<script type="text/javascript" src="./lib/jquery.mcdropdown.js"></script>
<script type="text/javascript" src="./lib/jquery.bgiframe.js"></script>
<!---// load the mcdropdown css stylesheet //--->
<link type="text/css" href="./css/jquery.mcdropdown.css" rel="stylesheet" media="all" />
<script type="text/javascript">
$(document).ready(function (){
var delim = "#delim#";
dd= $("#category").mcdropdown("#categorymenu",{
allowparentselect:true,
delim:delim
});
});
</script>
</head>
<body>
<p>
please select a category:<br />
<input type="text" name="category" id="category" value="" />
</p>
#categorymenu#
</body>
</html>
test.php
<?php
//目录信息处理
$path_ref_count = 1;
$path_html_str ='';
openpath(".",true,&$path_html_str,&$path_ref_count);
$path_html_str = '<ul id="categorymenu" class="mcdropdown_menu">'.$path_html_str.'</ul>';
$path_html_str = str_replace ( "<ul></ul>", '', $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.='<li rel="'.$path_ref_count++.'">';
$path_html_str.=$file.'<ul>';
if($ifchild)
{
//递归
openpath($path.directory_separator.$file,$ifchild,&$path_html_str,&$path_ref_count);
}
$path_html_str.='</ul></li>';
}
}
}
}
closedir($handle);
}
?>
jquery mcdropdown 插件可以在这里下载:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
-
-
Yii框架应用组件用法实例分析
本文实例讲述了yii框架应用组件用法。分享给大家供大家参考,具体如下:应用组件 应用主体是, 它部署一组提供各种不同功能的 应用组件 来处理请求。 例如,url...
[阅读全文]
-
-
-
-
-
Yii 框架入口脚本示例分析
本文实例讲述了yii 框架入口脚本。分享给大家供大家参考,具体如下:入口脚本入口脚本是应用启动流程中的第一环, 一个应用(不管是网页应用还是控制台应用)只有一个...
[阅读全文]
-
PHP强制转化的形式整理
php强制转化有几种形式php强制转化有3种形式:1、在要转换的变量之前加上用括号括起来的目标类型;2、使用转强函数,如“intval()”,“strval()...
[阅读全文]
-
php7 新增功能实例总结
本文实例总结了php7 新增功能。分享给大家供大家参考,具体如下:一、核心 增加了group use语法声明。rfc: https://wiki.php.ne...
[阅读全文]
网友评论