Home  >  Article  >  Backend Development  >  How to delete folders recursively in php

How to delete folders recursively in php

藏色散人
藏色散人Original
2020-11-05 13:54:442523browse

php method to recursively delete a folder: first create a PHP sample file; then determine the directory of the file to be deleted; then open the specified directory through opendir; and finally delete the folder through recursive method.

How to delete folders recursively in php

Recommended: "PHP Video Tutorial"

php recursively delete folders

The code is as follows :

<?php
// $dir:要删除的文件的目录
function recursiveDelete($dir){ 
// 打开指定目录 
        if ($handle = @opendir($dir)) { 
            while (($file = readdir($handle)) !== false) {
                   if (($file == ".") || ($file == "..")) { continue; }
                   if (is_dir($dir . &#39;/&#39; . $file)) {
                         // 递归 
                         recursiveDelete($dir . &#39;/&#39; . $file); 
                   } else { 
                         unlink($dir . &#39;/&#39; . $file); 
                         // 删除文件 
                  } 
            } 
           @closedir($handle); 
           rmdir ($dir); 
      }
}

The above is the detailed content of How to delete folders recursively 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