ThinkPHP删除某个文件夹下的所有文件
/*此方法为公共方法用来删除某个文件夹下的所有文件 * $path为文件的路径 * $fileName文件夹名称 * */ public function rmFile($path,$fileName){ //去除空格 $path = preg_replace('/(\/){2,}|{\\\}{1,}/','/',$path); //得到完整目录 $path.= $fileName; //判断此文件是否为一个文件目录 if(is_dir($path)){ //打开文件 if ($dh = opendir($path)){ //遍历文件目录名称 while (($file = readdir($dh)) != false){ //逐一进行删除 unlink($path.'\\'.$file); } //关闭文件 closedir($dh); } } }
//一对一删除缓存 public function cache(){
//前台用ajax get方式进行提交的,这里是先判断一下 if($_GET['type']){
//获取提交过来的值(也就是要删除的文件夹名称)
$type=$_GET['type'];
//获取文件的绝对路径
$abs_dir=dirname(dirname(dirname(dirname(__FILE__))));
//与要删除的文件夹里的文件进行组合 $pa=$abs_dir.'\Admin\Runtime\\';
//调用上面写好的方法
$this->rmFile($pa,$type);
//返回提示信息 $this->ajaxReturn(1,'清除成功',1); }else{ $this->display(); } }
//一键清除所有缓存 public function allrun(){
////前台用ajax get方式进行提交的,这里是先判断一下 if($_GET['type']){
//得到传递过来的值 $type=$_GET['type'];
//将传递过来的值进行切割,我是已“-”进行切割的 $name=explode('-', $type);
//得到切割的条数,便于下面循环
$count=count($name);
//循环调用上面的方法
for ($i=0;$i<$count;$i++){
//得到文件的绝对路径
$abs_dir=dirname(dirname(dirname(dirname(__FILE__))));
//组合路径 $pa=$abs_dir.'\Admin\Runtime\\';
//调用删除文件夹下所有文件的方法 $this->rmFile($pa,$name[$i]); }
//给出提示信息
$this->ajaxReturn(1,'清除成功',1); }else{ $this->display(); } }
前台页面很简单 在这里就给出重要的代码
html文件
//这个是ajax get方式提交
<script type="text/javascript" src="__PUBLIC__/scripts/jquery-1.7.1.min.js"></script> < script type="text/javascript"> $(function(){ $('#button').click(function(){ if(confirm("确认要清除模版缓存目录?")) { var $type=$('#type').val(); var $mess=$('#mess'); $.getJSON('__URL__/cache',{type:$type},function(json){ if(json.status==1){ $mess.slideDown(3000,function(){ $mess.css('color:red'); }).html(json.info); }else{ $mess.slideDown(3000,function(){ $mess.css('color:red'); }).html(json.info); } }); }else{ return false; } }); }); < /script>
<form action="" method="get"> <table border="0" cellpadding="2" cellspacing="1" style="width:100%">
<tr>
//关键就是这个隐藏域的默认值 就是你要删除文件夹里面内容的文件夹的名
<input type="hidden" name="type" id="type" class="text" style="width:50px" value="Cache" /> <td nowrap align="left"><input type="button" id="button" class="text" style="width:100px;height:50px;" value="点击清除模版缓存目录" /></td> <td id="mess"></td> </tr> </table>
</form>
//一键清除所有
<form action="" method="get"> <table border="0" cellpadding="2" cellspacing="1" style="width:100%"> <tr>
//默认值是几个文件夹的名字连起来 提交方式和上面的一样 <input type="hidden" name="type" id="type" class="text" style="width:50px" value="Cache-Data-Temp-Logs" /> <td nowrap align="left"><input type="button" id="button" class="text" style="width:100px;height:50px;" value="点击一键清除所有" /></td> <td id="mess"></td> </tr> </table> </form>
浙公网安备 33010602011771号