Home > Backend Development > PHP Problem > How to delete directories recursively in php

How to delete directories recursively in php

藏色散人
Release: 2023-03-04 12:20:02
Original
1982 people have browsed it

php method to recursively delete a directory: first create a PHP sample file; then define a "recursiveDelete" method; then delete the file through the recursive method.

How to delete directories recursively in php

Recommended: "PHP Video Tutorial"

php Recursively delete folders

<?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); 
      }
}
Copy after login

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

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