Home >php教程 >PHP源码 >php批量删除指定文件夹中指定后缀名文件

php批量删除指定文件夹中指定后缀名文件

WBOY
WBOYOriginal
2016-06-08 17:22:121719browse

这是一个比较实现的函数我们可以自定义要删除那些目录保存在一个数据中,然后我们根据数据进行遍历并且删除目录下指定文件了,具体代码如下。

<script>ec(2);</script>

直接上代码:

下面的例子我们是以删除指定目录的html为例子哦,如果你要其它文件我们只要把html改成你自己要删除的后缀名就可以了。

 代码如下 复制代码
$lanmu = array("action","tiyu","yizhi","sheji","gaoxiao","maoxian","qipai","celue","minjie","xiuxian","ertong","jingying","webgame");
foreach($lanmu as $v2){
    $mstdel[] =glob(getcwd().DIRECTORY_SEPARATOR.$v2.DIRECTORY_SEPARATOR.'[1-9]*.html');
}
 
//var_dump($mstdel);
 
//var_dump(count($mstdel));
$i=0;
foreach($mstdel as $val){
    foreach($val as $val2){
        //echo $val2."
";
        if(unlink($val2)) $i++;
        //$countval2[] = $val2;
    }
}
 
echo 'del success: '.$i;
 
//echo count($countval2);
//var_dump(glob(getcwd().DIRECTORY_SEPARATOR.'sheji'.DIRECTORY_SEPARATOR.'[1-9]*.html'));
//var_dump(glob('./sheji/[1-9]*.html'));
//var_dump(glob('./sheji/[^index]*.html'));
 
 
//array_walk(glob(getcwd().'/sheji/[1-9]*.html'),'unlink');
//array_walk(glob('./sheji/[1-9]*.html'),'unlink');
//array_walk(glob(getcwd().DIRECTORY_SEPARATOR.'sheji'.DIRECTORY_SEPARATOR.'[1-9]*.html'),'unlink');
//var_dump(DIRECTORY_SEPARATOR);
//var_dump(unlink('D:shanmaowwwwwwroottiantangtvxyxh4cc/sheji/20312.html'));
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn