下面给大家提供几个函数参考。
实例一:
<?php <BR>function deletedir($dir){ <BR> if(!handle=@opendir($dir)){ //检测要打开目录是否存在 <BR> die("没有该目录"); <BR> } <BR> while(false !==($file=readdir($handle))){ <BR> if($file!=="."&&$file!==".."){ //排除当前目录与父级目录 <BR> $file=$dir .DIRECTORY_SEPARATOR. $file; <BR> if(is_dir($file)){ <BR> deletedir($file); <BR> }else{ // www.php.net<BR> if(@unlink($file)){ <BR> echo "文件<b>$file</b>删除成功。<br>"; <BR> }else{ <BR> echo "文件<b>$file</b>删除失败!<br>"; <BR> } <BR> } <BR> } <BR> if(@rmdir($dir)){ <BR> echo "目录<b>$dir</b>删除成功了。<br>\n"; <BR> }else{ <BR> echo "目录<b>$dir</b>删除失败!<br>\n"; <BR> } <BR>} <br><br>//测试程序 <BR>$dir="/var/www/test"; <BR>deletedir($d<em style="color:transparent">本文来源[email protected]搞@^&代*@码)网9</em><strong>搞代gaodaima码</strong>ir); <BR>?> <BR>
实例二:Php递归删除目录方法(兼容中文目录和文件)
<?php <BR>/*<BR>本函数支持中文删除目录,由于我只测试了window下删除,linux未测试,如有问题可留言或者自己稍加修改即可。<BR>如有错误欢迎大家指正,共同学习<BR>*/<BR>header("Content-type: text/html; charset=gb2312");<br><br>function delete_dir($tmp_path){<br><br> <BR> if(!is_writable($tmp_path) && is_dir($tmp_path)){<BR> chmod($tmp_path,0777);<BR> }<br><br> $encode = mb_detect_encoding($tmp_path, array('UTF-8','GB2312','ASCII','GBK'));<BR> $tmp_path = iconv($encode,'gb2312',$tmp_path);<br><br> <BR> $handle_object = scandir($tmp_path);<br><br> <BR> if(count(scandir($tmp_path))==2){<br><br> if(rmdir($tmp_path)){<BR> echo $tmp_path.'<br />';<BR> }else{<BR> echo $tmp_path.'<br />';<BR> }<br><br> return ;<br><br> }<br><br> foreach($handle_object as $val){ <br><br> if($val!='..' && $val!='.' && $val!=''){<br><br> if(filetype($tmp_path.'/'.$val)=='dir'){<br><br> if(count(scandir($tmp_path.'/'.$val))==2){<BR> if(rmdir($tmp_path.'/'.$val)){<BR> echo $$tmp_path.'/'.$val.'<br />';<BR> }else{<BR> echo $$tmp_path.'/'.$val.'<br />';<BR> }<BR> }else{<BR> delete_dir($tmp_path.'/'.$val);<BR> } <BR> }else{<BR> if(unlink($tmp_path.'/'.$val)){<BR> echo $$tmp_path.'/'.$val.'<br />';<BR> }else{<BR> echo $$tmp_path.'/'.$val.'<br />';<BR> }<BR> } <BR> }else{<br><br> continue;<BR> }<br><br> }<br><br> <BR> if(rmdir($tmp_path)){<BR> echo $tmp_path.'<br />';<BR> }else{<BR> echo $tmp_path.'<br />';<BR> }<br><br> return ;<br><br> <BR>}<br><br>delete_dir('D:/AppServ/www/testing/哈哈');<br><br>?><BR>
实例三:参数$dir文件名例子:admin/runtime 这样的
//删除目录及所包含文件函数 <BR>function deldir($dir) { <BR> //打开文件目录 <BR> $dh = opendir($dir); <BR> //循环读取文件 <BR> while ($file = readdir($dh)) { <BR> if($file != '.' && $file != '..') { <BR> $fullpath = $dir . '/' . $file; <br><br> //判断是否为目录 <BR> if(!is_dir($fullpath)) { <BR> echo $fullpath."已被删除<br>"; <BR> //如果不是,删除该文件 <BR> if(!unlink($fullpath)) { <BR> } <BR> } else { <BR> //如果是目录,递归本身删除下级目录 <BR> deldir($fullpath); <BR> } <BR> } <BR> } <BR> //关闭目录 <BR> closedir($dh); <BR> //删除目录 <BR> //if(rmdir($dir)) { <BR> // return true; <BR>// } else { <BR> // return false; <BR> // } <BR>}<BR>
实例四:
<?php <BR>function deldir($dirname){ <BR> if(file_exists($dirname)){//首先判断目录是否有效 <BR> $dir = opendir($dirname);//用opendir打开目录 <BR> while($filename = readdir($dir)){//使用readdir循环读取目录里的内容 <BR> if($filename != "." && $filename != ".."){//排除"."和".."这两个特殊的目录 <BR> $file = $dirname."/".$filename; <BR> if(is_dir($file)){//判断是否是目录,如果是则调用自身 <BR> deldir($file); //使用递归删除子目录 <BR> }else{ <BR> @unlink($file);//删除文件 <BR> } <BR> } <BR> } <BR> closedir($dir);//关闭文件操作句柄 <BR> rmdir($dirname);//删除目录 <BR> } <BR>} <BR>?><BR>
实例五:
/**<BR> * 删除非空目录<BR> * @method rrmdir<BR> */<BR>function rrmdir ($dir) {</P><P> if (is_dir($dir)) {</P><P> $fs = array_slice(scandir($dir), 2);<BR> foreach ($fs as $f) {<BR> $path = $dir . '/' . $f;<BR> is_dir($path) ? rrmdir($path) : unlink($path);<BR> }<BR> reset($fs);<BR> return rmdir($dir);<BR> }</P><P>}<BR>
实例六:
<?php<BR>function del_dir( $dir )<BR>{<BR> if ( $handle = opendir( $dir ) )<BR> {<BR> while ( false !== ( $item = readdir( $handle ) ) )<BR> {<BR> if ( $item != "." && $item != ".." )<BR> {<BR> if ( is_dir( "$dir/$item" ) )<BR> {<BR> del_dir( "$dir/$item" );<BR> }<BR> else<BR> {<BR> unlink( "$dir/$item" ) ;<BR> }<BR> }<BR> }<BR> closedir( $handle );<BR> rmdir( $dir ) ;</P><P> }<BR>}<BR>?><BR>