Welcome Guest, Not a member yet? Register   Sign In
Recursive Remove Directory
#1

[eluser]Jay Logan[/eluser]
Had to piece together a helper that will allow me to delete a folder and all its contents (including subfolders). Here is the code I'm using in case anyone else would like use it.


recursive_helper.php in custom helpers folders.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('remove_directory'))
{
    function remove_directory($directory, $empty=FALSE)
    {
        if(substr($directory,-1) == '/') {
            $directory = substr($directory,0,-1);
        }

        if(!file_exists($directory) || !is_dir($directory)) {
            return FALSE;
        } elseif(!is_readable($directory)) {

        return FALSE;

        } else {

            $handle = opendir($directory);
            while (FALSE !== ($item = readdir($handle)))
            {
                if($item != '.' && $item != '..') {
                    $path = $directory.'/'.$item;
                    if(is_dir($path)) {
                        remove_directory($path);
                    }else{
                        unlink($path);
                    }
                }
            }
            closedir($handle);
            if($empty == FALSE)
            {
                if(!rmdir($directory))
                {
                    return FALSE;
                }
            }
        return TRUE;
        }
    }
}

/* End of file recursive_helper.php */
/* Location: ./system/helpers/recursive_helper.php */

Then you $this->load->helper('recursive') and run the remove_directory() function. Hope this helps.
#2

[eluser]drewbee[/eluser]
What was wrong with the file helper delete_files() ??

http://ellislab.com/codeigniter/user-gui...elper.html
#3

[eluser]Jay Logan[/eluser]
I tried it but it only deleted the files, not the actual folders.
#4

[eluser]drewbee[/eluser]
Yeah... you gotta pass TRUE as a second param to it.
#5

[eluser]Jay Logan[/eluser]
But that would only delete the folders and files WITHIN the specified folder. What if I want to delete the specified folder and everything (files and folders) in it? Will it work?
#6

[eluser]drewbee[/eluser]
Code:
if ( ! function_exists('delete_files'))
{
    function delete_files($path, $del_dir = FALSE, $level = 0)
    {    
        // Trim the trailing slash
        $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
        
        if ( ! $current_dir = @opendir($path))
            return;
    
        while(FALSE !== ($filename = @readdir($current_dir)))
        {
            if ($filename != "." and $filename != "..")
            {
                if (is_dir($path.'/'.$filename))
                {
                    // Ignore empty folders
                    if (substr($filename, 0, 1) != '.')
                    {
                        delete_files($path.'/'.$filename, $del_dir, $level + 1);
                    }
                }
                else
                {
                    unlink($path.'/'.$filename);
                }
            }
        }
        @closedir($current_dir);
    
        if ($del_dir == TRUE AND $level > 0)
        {
            @rmdir($path);
        }
    }
}

A quick look at the code sais yes. It removes all the files then deletes the directory if it is set to true.
#7

[eluser]Jay Logan[/eluser]
Right on. I'll give it a try.
#8

[eluser]Jay Logan[/eluser]
Just tried delete_files function with TRUE as second param and it did not remove source directory but it deleted all sub directories and files. Thanks anyway.
#9

[eluser]bcorcoran[/eluser]
Using this knowledge couldn't you create a much simpler helper file utilizing the built in delete_files() function?
#10

[eluser]Jay Logan[/eluser]
I'm sure someone could. I didn't write the above code. Found it on Google and turned it into a CI helper. Works fine for me. What I actually did was add the function into my base_controller.php (which I include in all my other controllers) then I access it by $this->remove_directory(PATH). But I figured most people don't have there CI set up in that way so I turned it into a helper file for others.

It seems like such a minor feature and I was surprised to see CI didn't already support it. But PHP doesn't have such a function either so...figures.




Theme © iAndrew 2016 - Forum software by © MyBB