When deleting a directory in php, if it is an empty directory, we can directly use rmdir to delete it. However, to delete a non-empty directory, we need to first delete the files in the most sub-directory and then delete the empty directory. In fact, it is deleting the directory recursively.
//Loop to delete directories and files function
The code is as follows |
Copy code |
代码如下 |
复制代码 |
function delDirAndFile( $dirName )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delDirAndFile( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item
n”;
}
}
}
closedir( $handle );
if( rmdir( $dirName ) )echo “成功删除目录: $dirName
n”;
}
}
?>
|
function delDirAndFile( $dirName )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delDirAndFile( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “Successfully deleted file: $dirName/$item
n”;
}
}
}
closedir( $handle );
if( rmdir( $dirName ) )echo "Directory deleted successfully: $dirName
n”;
}
}
?>
|
//Loop all files in the directory
代码如下 |
复制代码 |
function delFileUnderDir( $dirName=”../Smarty/templates/templates_c” )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delFileUnderDir( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item
n”;
}
}
}
closedir( $handle );
}
}
?>
|
The code is as follows |
Copy code |
function delFileUnderDir( $dirName=”../Smarty/templates/templates_c” )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delFileUnderDir( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “Successfully deleted file: $dirName/$item
n”;
}
}
}
closedir( $handle );
}
}
代码如下 |
复制代码 |
function my_del_dir($directory)
{
if (is_dir($directory) == false)
{
exit("The Directory Is Not Exist!");
}
$handle = opendir($directory);
while (($file = readdir($handle)) !== false)
{
if ($file != "." && $file != "..")
{
is_dir("$directory/$file")?
my_del_dir("$directory/$file"):
unlink("$directory/$file");
}
}
if (readdir($handle) == false)
{
closedir($handle);
rmdir($directory);
}
}
?>
|
?>
|
Directory deletion function
The code is as follows |
Copy code |
function my_del_dir($directory)
{
If (is_dir($directory) == false)
exit("The Directory Is Not Exist!");
$handle = opendir($directory);
while (($file = readdir($handle)) !== false)
If ($file != "." && $file != "..")
Is_dir ("$ Directory /$ File")?
my_del_dir("$directory/$file"):
unlink("$directory/$file");
If (readdir($handle) == false)
closedir($handle);
rmdir($directory);
}
?>
|
One written by myself
$sourceDir Directory path to delete
Call method DeleteDir(“images/abc”); //Delete abc directory
The code is as follows
代码如下 |
复制代码 |
function DeleteDir($sourceDir){
$succeed = true;
if(file_exists($sourceDir)){
$objDir = opendir($sourceDir);
while(false !== ($fileName = readdir($objDir))){
if(($fileName != “.”) && ($fileName != “..”)){
chmod(“$sourceDir/$fileName”,0777);
if(!is_dir(“$sourceDir/$fileName”)){
if(!unlink(“$sourceDir/$fileName”)){
$succeed = false;
break;
}
}
else{
DeleteDir(“$sourceDir/$fileName”);
}
}
}
if(!readdir($objDir)){
closedir($objDir);
if(!rmdir($sourceDir)){
$succeed = false;
}
}
}
return $succeed;
}
|
|
Copy code |
|
function DeleteDir($sourceDir){
$succeed = true;
if(file_exists($sourceDir)){
$objDir = opendir($sourceDir);
while(false !== ($fileName = readdir($objDir))){
if(($fileName != “.”) && ($fileName != “..”)){
chmod(“$sourceDir/$fileName”,0777);
if(!is_dir(“$sourceDir/$fileName”)){
if(!unlink(“$sourceDir/$fileName”)){
$succeed = false;
break;
}
}
else{
DeleteDir(“$sourceDir/$fileName”);
}
}
}
if(!readdir($objDir)){
if(!rmdir($sourceDir)){
$succeed = false;
}
}
}
return $succeed;
}
http://www.bkjia.com/PHPjc/631535.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631535.htmlTechArticleDelete a directory in php. If it is an empty directory, we can directly use rmdir to delete it, but to delete a non-empty directory, we You need to delete the files in the most sub-directory first and then delete the empty directory. In fact,...