Home > Article > Backend Development > How to delete directories recursively in php
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.
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 . '/' . $file)) { // 递归 recursiveDelete($dir . '/' . $file); } else { unlink($dir . '/' . $file); // 删除文件 } } @closedir($handle); rmdir ($dir); } }
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!