Remove directory and its sub folders & files in codeigniter

We can extend the core directory helper.  Creating a new file as MY_directory_helper.php and add the following code in helper file to remove directory and it’s sub folders &  files.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function remove_dir($current_dir, $except_dir = array()) {

    if($dir = @opendir($current_dir))
    {
        while (($f = @readdir($dir)) !== false)
        {
            if(!in_array($f, $except_dir))
            {
                if($f > '0' and @filetype($current_dir.$f) == "file")
                {
                    @unlink($current_dir.$f);
                }
                elseif($f > '0' and @filetype($current_dir.$f) == "dir")
                {
                
                    remove_dir($current_dir.$f."//", $except_dir);
                }
            
            }
        }
        
        @closedir($dir);
        @rmdir($current_dir);
    }

}


/* End of file MY_directory_helper.php */
/* Location: system/applications/helpers/MY_directory_helper.php */

 

Leave a Reply

Your email address will not be published. Required fields are marked *