Jun
17
2013

Remove a directory with all contents

/**
 * Function to delete all files in a given directory
 *
 * @access public
 * @param string $directory
 * @return void
 */
function delete_files( $directory )
{
    if ( !file_exists( $directory ) )
        return false;

    if ( is_dir( $directory ) && $dir_contents = opendir( $directory ) )
    {
        while ( false !== ( $single_file = readdir( $dir_contents ) ) )
        {
            unlink( $directory . '/' . $single_file );
        }
        closedir( $dir_contents );
    }

    return rmdir( $directory );
}

Leave a comment