3

I'm newbie laravel. I have a form register and contain field is avatar and some field need validate. I upload picture from my computer but when i submit form if some field not pass validate then picture file lost. How can i keep old picture uploaded ? With text field, i can use old() function to get old value but with input file i dont know how to keep value when form not pass validate.

(Have some suggestion: using ajax put value to session when input file change...)

Sorry my english skill not good, Thanks you.

Luther Keng
  • 41
  • 1
  • 3
  • Possible duplicate of [Laravel 5.1: keep uploaded file as old input](https://stackoverflow.com/questions/33158328/laravel-5-1-keep-uploaded-file-as-old-input) – Saad Suri Dec 04 '18 at 15:08

1 Answers1

5

No, the file input can't be prepopulated by Laravel or by any software. Your website (and any website) doesn't and shouldn't know the local path to the file. Imagine the security risks if they did! You could trick a user into uploading their SSH private key or something.

What you need to do is process the uploaded file regardless, even if there are validation errors.

Then you could either store it in your database with a pending status, or have some unique hash assigned to it, with a matching hash stored in the user's session. The point is to be able to identify incomplete uploads that belong to this specific user.

Then when you display the form, retrieve those incomplete files either from the session or database, and display the thumbnail next to the file upload. This tells the user that they don't need to re-upload that file. Just make sure that they have a way to remove it as well in case they changed their mind.

Once the form is submitted correctly then clear the session hash or update the database status to complete; whatever you need to do.

Andrewtweber Answered Very Well So i pasted here.Credit goes to him Ref: Laravel 5.1: keep uploaded file as old input

Vision Coderz
  • 8,257
  • 5
  • 41
  • 57