tags:

views:

61

answers:

2

I can create a image folder when I create a category, so that I can upload images there.

Now I want to delete that folder when I delete the category.

Code for creating a folder is the following and works well.

function create(){
if ($this->input->post('name')){
    $this->MCats->addCategory();
    $folder = $this->input->post('name');
    $folder = strtolower($folder);
    $folder = str_replace(" ", "_", $folder);
    $folder = 'images/'.$folder;
    $this->_create_path($folder);
...
...
}


function _create_path($folder)
{
    // create dir if not exists
    $folder = explode( "/" , $folder );
    $mkfolder = "";
    //sets the complete directory path
    for(  $i=0 ; isset( $folder[$i] ) ; $i++ )
    {
        $mkfolder .= $folder[$i] . '/';
        if(!is_dir($mkfolder )) mkdir("$mkfolder");
    }
}

And I come up with the following code. But I am not sure how to use rmdir so that it will not remove images folder. I want to remove only child of images folder.

function delete($id){

$cat = $this->MCats->getCategory($id);
    // This will pull the name of category name.
$catname = $cat['name'];
$catname = strtolower($catname);
$catname = str_replace(" ", "_", $catname);
$catname = 'images/'.$catname;
    $this->_remove_path($catname);
...
...
}
function _remove_path($folder)
{

}

I am not sure how to proceed after this.

Can anyone give me some suggestions please?

+2  A: 
$this->_remove_path($catname); // because previous parts you're using $catname

Then the remove path function

// recursively remove all files and sub-folder in that particular folder
function _remove_path($folder){
    $files = glob( $folder . DIRECTORY_SEPARATOR . '*');
    foreach( $files as $file ){
        if($file == '.' || $file == '..'){continue;}
        if(is_dir($file)){
            $this->_remove_path( $file );
        }else{
            unlink( $file );
        }
    }
    rmdir( $folder ); 
}
thephpdeveloper
Can you explain what's going on in foreach? Please.
shin
it will loop through to see if there is directories or files in that sub-folder before deleting. it will then delete the sub-folder or files. rmdir will fail if there are still files or sub-folder in the folder that you want to delete.
thephpdeveloper
It works, thanks.
shin
no problem. happy new year!
thephpdeveloper
A: 

You'll need to used unlink and rmdir:

$handler = opendir($folder);
if (!$handler) {
    trigger_error('File Error: Failed to open the directory ' . $folder, E_USER_ERROR);
    return false;
}

// list the files in the directory
while ($file = readdir($handler)) {
    // if $file isn't this directory or its parent,
    if ($file != '.' && $file != '..' && !is_dir($file)) {
        // delete it
        if (!unlink($file)) {
            trigger_error('File Error: Failed to remove file ' . $file, E_USER_ERROR);
        }
    }
}

// tidy up: close the handler
closedir($handler);

if (!rmdir($folder)) {
    trigger_error('File Error: Failed to remove folder ' . $folder, E_USER_ERROR);
}
Darryl Hein