Home > Backend Development > PHP Tutorial > Delete the directory and subdirectories and files under the directory

Delete the directory and subdirectories and files under the directory

WBOY
Release: 2016-08-08 09:32:48
Original
998 people have browsed it
<?php
$Directory = "a/b";
function deleteDir($Directory){
	//检查目录是否存在,不存在则退出程序
	if(is_dir($Directory)){
		//打开目录
		$handle = openDir($Directory);
		//循环遍历目录
		while(($file_name = readdir($handle))!==false){
			//文件路径
			$file_path = $Directory.DIRECTORY_SEPARATOR.$file_name;
			//如果目录为 . 或 .. 则不执行下面代码
			if($file_name!="." && $file_name!=".."){
				//如果是目录
				if(is_dir($file_path)){
					//调用函数本身,递归遍历所有目录和文件
					deleteDir($file_path);
				}else{
					//删除文件
					unlink($file_path);
				}
			}
		}
		//关闭文件
		closedir($handle);
		//删除目录
		rmdir($Directory);
	}
 
}
deleteDir($Directory);
?>
Copy after login

The above introduces the deletion of directories and subdirectories and files under the directory, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template