PHP递归删除目录几个代码实例
下面给大家提供几个函数参考。
实例一:
<?php
function deletedir($dir){
if(!handle=@opendir($dir)){ //检测要打开目录是否存在
die("没有该目录");
}
while(false !==($file=readdir($handle))){
if($file!=="."&&$file!==".."){ //排除当前目录与父级目录
$file=$dir .DIRECTORY_SEPARATOR. $file;
if(is_dir($file)){
deletedir($file);
}else{ // www.jb51.net
if(@unlink($file)){
echo "文件<b>$file</b>删除成功。<br>";
}else{
echo "文件<b>$file</b>删除失败!<br>";
}
}
}
if(@rmdir($dir)){
echo "目录<b>$dir</b>删除成功了。<br>\n";
}else{
echo "目录<b>$dir</b>删除失败!<br>\n";
}
}
//测试程序
$dir="/var/www/test";
deletedir($dir);
?>
实例二:Php递归删除目录方法(兼容中文目录和文件)
<?php
/*
本函数支持中文删除目录,由于我只测试了window下删除,linux未测试,如有问题可留言或者自己稍加修改即可。
如有错误欢迎大家指正,共同学习
*/
header("Content-type: text/html; charset=gb2312");
function delete_dir($tmp_path){
if(!is_writable($tmp_path) && is_dir($tmp_path)){
chmod($tmp_path,0777);
}
$encode = mb_detect_encoding($tmp_path, array('UTF-8','GB2312','ASCII','GBK'));
$tmp_path = iconv($encode,'gb2312',$tmp_path);
$handle_object = scandir($tmp_path);
if(count(scandir($tmp_path))==2){
if(rmdir($tmp_path)){
echo $tmp_path.'<br />';
}else{
echo $tmp_path.'<br />';
}
return ;
}
foreach($handle_object as $val){
if($val!='..' && $val!='.' && $val!=''){
if(filetype($tmp_path.'/'.$val)=='dir'){
if(count(scandir($tmp_path.'/'.$val))==2){
if(rmdir($tmp_path.'/'.$val)){
echo $$tmp_path.'/'.$val.'<br />';
}else{
echo $$tmp_path.'/'.$val.'<br />';
}
}else{
delete_dir($tmp_path.'/'.$val);
}
}else{
if(unlink($tmp_path.'/'.$val)){
echo $$tmp_path.'/'.$val.'<br />';
}else{
echo $$tmp_path.'/'.$val.'<br />';
}
}
}else{
continue;
}
}
if(rmdir($tmp_path)){
echo $tmp_path.'<br />';
}else{
echo $tmp_path.'<br />';
}
return ;
}
delete_dir('D:/AppServ/www/testing/哈哈');
?>
实例三:参数$dir文件名例子:admin/runtime 这样的
//删除目录及所包含文件函数
function deldir($dir) {
//打开文件目录
$dh = opendir($dir);
//循环读取文件
while ($file = readdir($dh)) {
if($file != '.' && $file != '..') {
$fullpath = $dir . '/' . $file;
//判断是否为目录
if(!is_dir($fullpath)) {
echo $fullpath."已被删除<br>";
//如果不是,删除该文件
if(!unlink($fullpath)) {
}
} else {
//如果是目录,递归本身删除下级目录
deldir($fullpath);
}
}
}
//关闭目录
closedir($dh);
//删除目录
//if(rmdir($dir)) {
// return true;
// } else {
// return false;
// }
}
实例四:
<?php
function deldir($dirname){
if(file_exists($dirname)){//首先判断目录是否有效
$dir = opendir($dirname);//用opendir打开目录
while($filename = readdir($dir)){//使用readdir循环读取目录里的内容
if($filename != "." && $filename != ".."){//排除"."和".."这两个特殊的目录
$file = $dirname."/".$filename;
if(is_dir($file)){//判断是否是目录,如果是则调用自身
deldir($file); //使用递归删除子目录
}else{
@unlink($file);//删除文件
}
}
}
closedir($dir);//关闭文件操作句柄
rmdir($dirname);//删除目录
}
}
?>
实例五:
/**
* 删除非空目录
* @method rrmdir
*/
function rrmdir ($dir) {
if (is_dir($dir)) {
$fs = array_slice(scandir($dir), 2);
foreach ($fs as $f) {
$path = $dir . '/' . $f;
is_dir($path) ? rrmdir($path) : unlink($path);
}
reset($fs);
return rmdir($dir);
}
}
实例六:
<?php
function del_dir( $dir )
{
if ( $handle = opendir( $dir ) )
{
while ( false !== ( $item = readdir( $handle ) ) )
{
if ( $item != "." && $item != ".." )
{
if ( is_dir( "$dir/$item" ) )
{
del_dir( "$dir/$item" );
}
else
{
unlink( "$dir/$item" ) ;
}
}
}
closedir( $handle );
rmdir( $dir ) ;
}
}
?>
相关文章
在Mac OS的PHP环境下安装配置MemCache的全过程解析
这篇文章主要介绍了在Mac OS的PHP环境下安装配置MemCache的全过程解析,MemCache是一套分布式的高速缓存系统,需要的朋友可以参考下2016-02-02php 升级到 5.3+ 后出现的一些错误,如 ereg(); ereg_replace(); 函数报错
这篇文章主要介绍了php 升级到 5.3+ 后出现的一些错误,如 ereg(); ereg_replace(); 函数报错 的相关资料,需要的朋友可以参考下2015-12-12php序列化函数serialize() 和 unserialize() 与原生函数对比
这篇文章主要介绍了php序列化函数serialize() 和 unserialize() 与php原生序列化方法对比,有需要的小伙伴可以参考下。2015-05-05php getcwd与dirname(__FILE__)区别详解
这篇文章主要介绍了php getcwd与dirname(__FILE__)区别详解的相关资料,需要的朋友可以参考下2016-09-09CI框架中数据库操作函数$this->db->where()相关用法总结
这篇文章主要介绍了CI框架中数据库操作函数$this->db->where()相关用法,结合实例分析了or_where()、where_in()、or_where_in()、where_not_in()及or_where_not_in()等函数的使用技巧,需要的朋友可以参考下2016-05-05PHP url的pathinfo模式加载不同控制器的简单实现
下面小编就为大家带来一篇PHP url的pathinfo模式加载不同控制器的简单实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-08-08CentOS下PHP7的编译安装及MySQL的支持和一些常见问题的解决办法
这篇文章主要介绍了CentOS下PHP7的编译安装及MySQL的支持和一些问题的解决 的相关资料,需要的朋友可以参考下2015-12-12
最新评论