PHP – Delete Directory and Content Recursively

May 13th, 2012 by Laeeq | 8 comments

Here is a simple php code that will delete the directory and its content recursively.

Example Code :

  1. <?php
  2. function rrmdir($dir){
  3. if (is_dir($dir)){
  4. $objects = scandir($dir);
  5. foreach ($objects as $object){
  6. if ($object != “.” && $object != “..”){
  7. if (filetype($dir.“/”.$object) == “dir”) rrmdir($dir.“/”.$object); else unlink($dir.“/”.$object);
  8. }
  9. }
  10. reset($objects);
  11. rmdir($dir);
  12. }
  13. }
  14. ?>

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Wrapping Text using PHP
  • PHP 5.5 beta 4 is now available
  • Swapping Array Keys and Values
  • PHP Security
  • Working with Image Metadata in PHP
  • Converting Between Relative and Absolute File Path
  • Parsing File Paths with PHP
  • How to remove HTML Comments with PHP?
  • How to access .htaccess values?
  • Dealing With Common PHP Errors
  • Interview Questions and Answers for Freshers
  • Display numbers with ordinal suffix using PHP
  • Function to set COLLATION on database fields of Mysql
  • Read and write to remote files using PHP
  • Sharing PHP SESSION Variables between Multiple Subdomains
  • PHP 5.5.0 Alpha4 Development Preview Released
  • Check a string starts with http using PHP
  • Cross-Site Scripting Attacks (XSS)
  • PHP 5.4.11 and PHP 5.3.21 released!
  • PHP Memory Leak Issue
  •  

     

    1. robertyounguj
      September 28th, 2012 at 11:59 | #1

      I just couldn’t leave your web site prior to suggesting that I really enjoyed the standard info an individual supply in your guests? Is going to be again frequently to check up on new posts.

    2. josephadamsze
      September 28th, 2012 at 15:47 | #2

      Nice read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch as I found it for him smile Therefore let me rephrase that: Thanks for lunch! “Not only is the universe stranger than we imagine, it is stranger than we can imagine.” by Sir Arthur Eddington.

    3. davidsanchezki
      October 3rd, 2012 at 05:21 | #3

      I’ll right away grasp your rss as I can’t find your email subscription hyperlink or e-newsletter service. Do you’ve any? Kindly permit me realize in order that I may just subscribe. Thanks.

    4. melaniepetriezs
      October 3rd, 2012 at 11:38 | #4

      Thanks, I’ve just been searching for info about this topic for ages and yours is the greatest I’ve discovered so far. However, what concerning the bottom line? Are you certain in regards to the supply?

    5. williamperryqv
      October 3rd, 2012 at 12:09 | #5

      I gotta favorite this site it seems very useful very beneficial

    6. November 7th, 2012 at 10:24 | #6

      I’m very pleased to uncover this web site. I need to to thank you for ones time for this particularly fantastic read!! I definitely appreciated every part of it and i also have you bookmarked to look at new things in your site.

    7. online-jobs
      December 5th, 2012 at 11:10 | #7

      I consider something truly fascinating about your internet site so I saved to fav.

    8. poker bets uk
      December 9th, 2012 at 19:37 | #8

      This really is a excellent weblog, would you be involved in performing an interview about just how you designed it? If so e-mail me!

    1. No trackbacks yet.