FriendsOfCake/cakephp-upload

Enable deleteCallback to delete folders

marcheimendinger opened this issue · 4 comments

I use a webroot{DS}files{DS}{time}{DS} path. So for each file I create a folder with the current time. When I delete a file, I also want to delete the parent directory (webroot{DS}files{DS}{time}).

I tried by using a deleteCallback :
return [$path . $entity->name, $path];

Sadly it doesn't seem to work on folders but only on files. Would it be possible to add this feature to the deleteCallback method ?

Why can't you just make your own callback method which implements unlink() and rmdir() ?

The whole point of a callback method is to allow flexibility for you to implement your own code.

Deletes are dangerous, especially if you delete a folder, so handling it in the users code would be more logical in my opinion.

For someone looking for something similar, this is how I'm doing it :

public function afterDelete($event, $entity, $options)
{
    $completeDir = WWW_ROOT . str_replace("webroot" . DS, "", $entity->dir);
    $folder = new Folder($completeDir);
    $files = $folder->find('.', false);
    if (empty($files)) {
        $folder->delete();
    }
}