Home  >  Article  >  php教程  >  最简单删除目录与文件php代码

最简单删除目录与文件php代码

WBOY
WBOYOriginal
2016-05-25 16:44:04774browse

今天看到两行代码就可以删除指定目录下的所有文件与目录了这个非常的简单,有需要的朋友可进入参考一下

<?php
 
 //删除目录下所有空目录
 array_map(&#39;rmdir&#39;, glob(&#39;*&#39;, GLOB_ONLYDIR));
 
 //删除目录所有文件
 array_map(&#39;unlink&#39;, array_filter(glob(&#39;*&#39;), &#39;is_file&#39;));
?>

原理分析

array_map('rmdir', glob('*', GLOB_ONLYDIR));

删除简单是使用了array_map函数,它的作用是返回用户自定义函数作用后的数组。回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致

而glob是遍历目录,然后把返回数组给了rmdir进行目录删除,然后

array_map('unlink', array_filter(glob('*'), 'is_file'));

原理差不多了,就是遍历目录之后我们再删除指定目录中的文件即可。

教程链接:

随意转载~但请保留教程地址★

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