How do I remove a directory that is not empty

0 votes
I am trying to remove a directory with rmdir, but I received the 'Directory not empty' message, because it still has files in it.

What function can I use to remove a directory with all the files in it as well?
Nov 4, 2020 in PHP by kartik
• 37,510 points
3,762 views

1 answer to this question.

0 votes

Hello,

Try this code:

function rrmdir($dir)
{
 if (is_dir($dir))
 {
  $objects = scandir($dir);

  foreach ($objects as $object)
  {
   if ($object != '.' && $object != '..')
   {
    if (filetype($dir.'/'.$object) == 'dir') {rrmdir($dir.'/'.$object);}
    else {unlink($dir.'/'.$object);}
   }
  }

  reset($objects);
  rmdir($dir);
 }
}

Hope it helps!!

answered Nov 4, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
1 answer

How do I check whether a checkbox is checked in jQuery?

Hello @kartik, The checked property of a checkbox DOM element ...READ MORE

answered Aug 28, 2020 in PHP by Niroj
• 82,880 points
589 views
0 votes
1 answer

How do I make a PHP form that submits to self?

Hello @kartik, The proper way would be to ...READ MORE

answered Nov 6, 2020 in PHP by Niroj
• 82,880 points
3,028 views
0 votes
1 answer

How do check if a PHP session is empty?

Hii, I would use isset and empty: session_start(); if(isset($_SESSION['blah']) && !empty($_SESSION['blah'])) { ...READ MORE

answered Nov 9, 2020 in PHP by Niroj
• 82,880 points
8,794 views
0 votes
1 answer

How do I remove quotes from a string?

Hello @kartik, Try this: str_replace('"', "", $string); str_replace("'", "", $string); Otherwise, ...READ MORE

answered Nov 16, 2020 in PHP by Niroj
• 82,880 points
17,514 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,881 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,684 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,548 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,740 views
0 votes
1 answer

How can I use PHP to check if a directory is empty?

Hello @kartik, It seems that you need scandir instead of ...READ MORE

answered Nov 10, 2020 in PHP by Niroj
• 82,880 points
8,783 views
0 votes
1 answer

How do I recursively delete a directory and its entire contents files as well as sub dirs in PHP?

Hello @kartik, The user-contributed section in the manual ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,880 points
796 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP