Vichuploaderbundle: How to delete a file (not delete entity object and not overwrite with another file)?

Created on 16 Oct 2014  路  6Comments  路  Source: dustin10/VichUploaderBundle

Hello,

I'm trying to figure out how to simply delete a file attached to an entity. I do not want to delete the entire entity object and neither do I want to replace the file with another file. Just delete the file. I searched the documentation and tried Google but didn't find anything useful. I also tried to set the file variable of the entity object to NULL and persist the entity. But this did not remove the physical file.

Any hints are highly appreciated.

Best,
Andreas

Support

Most helpful comment

I believe this behaviour of not deleting the physical file when an entity object's file variable is set null is rather a bug or a missing feature. I think that with

delete_on_update: true

setting the variable to null should be captured by the listener and the file should be deleted automatically.

All 6 comments

I posted too quickly. I solved this with the following code:

$this->get('vich_uploader.storage')->remove($entity, 'file_mapping');
$entity->setFile(null);
$entity->setFileName(null);

Followed by persisting the entity.

I close this issue then :)

I believe this behaviour of not deleting the physical file when an entity object's file variable is set null is rather a bug or a missing feature. I think that with

delete_on_update: true

setting the variable to null should be captured by the listener and the file should be deleted automatically.

This should be mentioned in the documentation somewhere :-)

The bundled evolved and the correct way to remove a file "manually" is now: $this->get('vich_uploader.upload_handler')->remove($entity, 'fieldName');

Injection of vich_uploader.upload_handler breaks the abstraction. Please consider implementing this:

setting the variable to null should be captured by the listener and the file should be deleted automatically.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benIT picture benIT  路  3Comments

smilesrg picture smilesrg  路  6Comments

WassabiVl picture WassabiVl  路  3Comments

NicolaPez picture NicolaPez  路  6Comments

seddighi78 picture seddighi78  路  4Comments