Laravel 5.1: Keep Uploaded File as Old Input

Laravel 5.1: keep uploaded file as old input

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.

Form::file: How to repopulate with Input::old After Validation Errors and/or on Update?

Apparently you can't.
Browsers don't allow you to set a value on a file input for security reasons.

There are two silimar questions, not laravel related but i think answers could help you:

Can you re-populate file inputs after failed form submission with PHP or JavaScript?

Restoring the value of a input type=file after failed validation

Laravel old input of selected option

I believe that when you use it in a string the blade directive gets as a string but it is not getting evaluated, have you tried this instead:

{{ old('color') == trans('s/w + rot') ? "selected" : "" }}

How to keep old image if a new one isn't uploaded on update. Laravel

I am assuming that you keep the name saved for the old image,

You can do like this,

$post_image = Str::slug($post->post_title); // taking from your old Post model instance lets say $post = Post::find(%id);

if ($request->hasFile('post_avatar')){
$image_path = public_path("/postImages/".$post_image);
if (File::exists($image_path)) {
File::delete($image_path);
}
$postimg = $request->file('post_avatar');
$postImgName = Str::slug($request->post_title) . '.' . $postimg->getClientOriginalExtension();
$destinationPath = public_path('/postImages');
$imagePath = $destinationPath. "/". $postImgName;
$postimg->move($destinationPath, $postImgName);
$post->post_avatar = $postImgName;
} else{
$post->post_avatar = $post_image;
}
$post->save();

Edit :

$post_image = Str::slug($post->post_title);

to

$post_image = $post->post_title;

How to show the input type file value after form is submited and there are errors?

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.

Reference https://stackoverflow.com/a/33158509/7809177

Laravel 5.1 file upload error

Don't forget to add enctype="multipart/form-data" to your <form> element.

If you're using Laravel Collective's Form/Html package, you can pass 'files' => true to the array:

{!! Form::open(['files' => true]); !!}


Related Topics



Leave a reply



Submit