Home  >  Article  >  Backend Development  >  PHP function to delete subdirectories recursively

PHP function to delete subdirectories recursively

墨辰丷
墨辰丷Original
2018-06-11 16:52:131387browse

This article mainly introduces the method of deleting the specified directory in PHP. It involves the skills of deleting the directory recursively in PHP. It is of great practical value. Friends in need can refer to it.

The example of this article tells the method of deleting the specified directory in PHP. method. The specific analysis is as follows:

<?php
/**
 * Delete a file, or a folder and its contents 
 * (recursive algorithm)
 * @author Aidan Lister <aidan@php.net>
 * @version 1.0.3
 * @param string  $dirname Directory to delete
 * @return bool Returns TRUE on success, FALSE on failure
 */
function rmdirr($dirname)
{
 // Sanity check
 if (!file_exists($dirname)) {
  return false;
 }
 // Simple delete for a file
 if (is_file($dirname) || is_link($dirname)) {
  return unlink($dirname);
 }
 // Loop through the folder
 $dir = dir($dirname);
 while (false !== $entry = $dir->read()) {
  // Skip pointers
  if ($entry == &#39;.&#39; || $entry == &#39;..&#39;) {
   continue;
  }
  // Recurse
  rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
 }
 // Clean up
 $dir->close();
 return rmdir($dirname);
}
?>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php techniques for caching through file storage

php is dynamically created based on an array Method of html code

PHP Mysql jQuery implements password retrieval function

The above is the detailed content of PHP function to delete subdirectories recursively. 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