beans_remove_dir

Remove a directory and its files.

beans_remove_dir( string $dir_path )

Return: (bool) Will always return true.

Parameters

NameTypeRequiredDefaultDescription
$dir_pathstringtrue-Path to directory to remove.

Source

function beans_remove_dir( $dir_path ) {

	if ( ! is_dir( $dir_path ) ) {
		return false;
	}

	$items = scandir( $dir_path );
	unset( $items[0], $items[1] );

	foreach ( $items as $needle => $item ) {

		$path = $dir_path . '/' . $item;

		if ( 'dir' === filetype( $dir_path . '/' . $item ) ) {
			beans_remove_dir( $path );
		} else {
			@unlink( $path );
		}

		unset( $items[ $needle ] );

	}

	@rmdir( $dir_path );

	return true;

}