Home  >  Article  >  Backend Development  >  How to delete a folder in php

How to delete a folder in php

藏色散人
藏色散人Original
2020-11-04 09:26:094382browse

php method to delete a folder: first create a PHP sample file; then delete the files in the directory and delete the current folder through "function deldir($dir) {...}".

How to delete a folder in php

Recommended: "PHP Video Tutorial"

php delete the folder and all files under the folder

<?
function deldir($dir) {
  //先删除目录下的文件:
  $dh=opendir($dir);
  while ($file=readdir($dh)) {
    if($file!="." && $file!="..") {
      $fullpath=$dir."/".$file;
      if(!is_dir($fullpath)) {
          unlink($fullpath);
      } else {
          deldir($fullpath);
      }
    }
  }
 
  closedir($dh);
  //删除当前文件夹:
  if(rmdir($dir)) {
    return true;
  } else {
    return false;
  }
}
?>

Example: Delete all ".svn" folders under a certain folder (including their contents must also be deleted).

<?php
function delsvn($dir) {
  $dh=opendir($dir);
  //找出所有".svn“ 的文件夹:
  while ($file=readdir($dh)) {
    if($file!="." && $file!="..") {
      $fullpath=$dir."/".$file;
      if(is_dir($fullpath)) {
            if($file==".svn"){
                delsvndir($fullpath);
            }else{
                delsvn($fullpath);
            }
      }
    }
  }
 
  closedir($dh);
}
function delsvndir($svndir){
    //先删除目录下的文件:
    $dh=opendir($svndir);
    while($file=readdir($dh)){
        if($file!="."&&$file!=".."){
            $fullpath=$svndir."/".$file;
            if(is_dir($fullpath)){
                delsvndir($fullpath);
            }else{
                unlink($fullpath);
            }
        }
        
    }
    closedir($dh);
    //删除目录文件夹
    if(rmdir($svndir)){
        return  true;
    }else{
        return false;
    }
    
}
$dir=dirname(__FILE__);
//echo $dir;
delsvn($dir);
?>

The above is the detailed content of How to delete a folder in php. For more information, please follow other related articles on the PHP Chinese website!

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