Delete file without use?

Friends, in my project I have some forms where I upload images but in some cases I have errors or I show an error message to the user at the end, maybe a field is incorrect or something and he decides to update the page, the problem I have here is that the file that was uploaded was saved in a folder but it was not related or saved in the db, so I want to see a way to delete those files, is there a way to do this? in an automated way? Thank you
No description
9 Replies
awcodes
awcodes4mo ago
How are you handling the upload? No files should get saved anywhere other than livewire-tmp until the form is actually saved successfully. And the tmp folder is purged every 24 hours so it doesn’t matter if they exist there.
TranceCode
TranceCode4mo ago
i have my code like this:
Forms\Components\FileUpload::make('image')
->label('Imagen')
->image()
->placeholder('Ingrese una imagen en formato gif')
->required(),
Forms\Components\FileUpload::make('image')
->label('Imagen')
->image()
->placeholder('Ingrese una imagen en formato gif')
->required(),
Sorry but i don't know if need make other configuration in other file!
awcodes
awcodes4mo ago
Ok. So the file in this case won’t get persisted until the form the FileUpload is part of is saved successfully. So you could see any files in the storage that are part of a form that fails validation.
TranceCode
TranceCode4mo ago
But does this mean I need to do some validation or add it before uploading the file and saving the record? Or how it is now is fine, and when we move it to production, what you mentioned will happen, that the tmp will be cleaned every 24 hours and those files will be deleted? Sorry for the questions, I have not done these configurations before.
awcodes
awcodes4mo ago
Ok, let’s see if I can explain the normal flow. I don’t have any insight to your actual app. But, the way livewire fileuploads work is that the uploaded file is saved to a tmp directory ( it’s called side loading) now when a form that has a FileUpload field in its schema it submitted, assuming all validation passes, the file is then moved from the tmp directory to the permanent directory as part of the saving process. So, unless you are doing anything custom for validation that might go against Laravel conventions those uploaded files will never get moved to permanent storage. Meaning that if you are doing everything right there shouldn’t be any orphaned files in the permanent storage. So, any file that is not moved from tmp storage to permanent storage will get deleted every 24 hours unless you have told livewire not to do that.
TranceCode
TranceCode4mo ago
oooh, ok, now can understand, i will check this configuration and test of course, thank you so much for the explanation and details... 👏🏼
awcodes
awcodes4mo ago
Maybe give this a good read too. https://livewire.laravel.com/docs/uploads
Laravel
File Uploads | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
awcodes
awcodes4mo ago
My Curator plugin is also open source if you want to see more examples of interacting with media. Not that it directly applies to your use case, but maybe there’s some insight in there. Cheers.
TranceCode
TranceCode4mo ago
Thank you so much @awcodes this will help me a lot... 🙌 💪