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

Create directory function...

Create directory if not exists. function createDir( $dir ) { if ( !is_dir( $dir ) ) { mkdir( $dir, 0777 ); return true; } return false; }...

Encrypt and Decrypt function with different outputs...

Encrypt function return different value with each execute but decrypt function return input value. for example: first execute: encrypt('hello') => 'axxdfg34', decrypt('axxdfg34') return 'hello'...

Resize image function...

Resize image function for jpg format. $mood between max and min. if you choose max, this mean max size of image is $ta otherwise min size of image is $ta. function makeTheThumb( $ppath, $ta, $mood ) ...

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...