FriendsOfCake / cakephp-upload

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

delete video file on deleting record from table #460

Closed anuj9196 closed 7 years ago

anuj9196 commented 7 years ago

When record is deleting from database, the file is not deleting,

File should be deleted whenever record is deleted from database

josegonzalez commented 7 years ago

Can you provide a reproduction of this?

anuj9196 commented 7 years ago

Sorry, I think I couldn't understand what you are asking for. Can you be a little clear ?

Meanwhile, I can elaborate my question more.

I have added added behaviour into my table

$this->addBehavior('Josegonzalez/Upload.Upload', [
       'video_file' => [
            'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}{microtime}{DS}',
            'fields' => [
                 'dir' => 'dir', // defaults to `dir`
             ],
       ],
 ]);

Now, Whenever I create a new record, video is uploaded with every newEntity().

After sometime that record has to be deleted and user triggers delete() to delete that record.

The record is being deleted from database but associated video is still there in the filesystem which is of no use now

I think there should be option to auto delete file on delete()

josegonzalez commented 7 years ago

From the docs, you can set keepFilesOnDelete to false. If you are generating any extra files, you'll need to use the deleteCallback option as well.

anuj9196 commented 7 years ago
    $this->addBehavior('Josegonzalez/Upload.Upload', [
            'video_file' => [
                'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}{microtime}{DS}',
                'fields' => [
                    'dir' => 'dir', // defaults to `dir`
                ],
                'keepFilesOnDelete' => false,
            ],
        ]);

added keepFilesOnDeleted => false, still files are not deleting

davidyell commented 7 years ago

Ooh, this could be related to #410 due to your use of microtime in your path. As the path to your file will change every microsecond.

anuj9196 commented 7 years ago

But what if path is saved to database, once path is generated you can take entity and path record from database before deletion is triggered and delete the file.

davidyell commented 7 years ago

That's what the suggestion was in #409 and #410. I guess you'll need to wait for the pull request to be merged 😄

josegonzalez commented 7 years ago

3.7.0 is out and contains the fix.