Delete or Remove all files and folders in a directory‎

If you want to delete everything from folder (including subfolders) use this function.

function removeDir( $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 );
   }
 
   return true;
}
 
removeDir( '/foo/' );

Tags

No tag here.

Recommended pages

Change timestamp to custom format...

Use this function for change timestamp to custom format......

Better parse_str in php...

parse_str() is fine for simple stuff but it's not the same as PHP's built way of creating the $_GET magic variable. Why?!?......

Detect file extension function...

This function is used to find the file extension also you can use pathinfo() function for get information about a file path. function ext($file_name) { return substr($file_name, strrpos($fi...

Extract numbers from a unicode string...

Quick extract numbers from a unicode string......