FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

Enable deleteCallback to delete folders #523

Closed marcheimendinger closed 4 years ago

marcheimendinger commented 4 years ago

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 ?

davidyell commented 4 years ago

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.

marcheimendinger commented 4 years ago

Of course, but it would be neat to integrate this into the plugin so we can do it as described (juste add the folder dir into the deleteCallback returned array).

On 25 Oct 2019, at 10:32, David Yell notifications@github.com wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

davidyell commented 4 years ago

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

marcheimendinger commented 4 years ago

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();
    }
}