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
Retrieve Data from Db and Display It in Table in PHP .. See This Code Whats Wrong with It
Use Curly Brackets to Structure Code in PHP
Differencebetween PHP Require and Include
Enabling 'Strict_Types' Globally in PHP 7
Get All Orders Ids from a Product Id in Woocommerce
Xampp: Couldn't Start Apache (Windows 10)
How to Inject a Repository into a Service in Symfony
Print Currency Number Format in PHP
Split a Text into Single Words
PHP and Microsoft Access Database - Connection and Crud
Is There a JavaScript Way to Do File_Get_Contents()
PHP Method Argument Type Hinting with Question Mark (Type)
Multiple Replace (Probably Preg_Replace) of Same String with Array
Is Is Bad Practice to Use Array_Walk with MySQLi_Real_Escape_String