Код отлично работает как файл, имя файла сохраняется в базе данных, файл сохраняется в назначенной папке, и его можно просмотреть позже. Единственная проблема, которая возникает иногда, - это 4 загрузки файла, при нажатии «Обзор» для поиска файла и выбора файла (2-й, 3-й или иногда при 4-м выборе), всплывающее окно выбора файла Windows останавливается, и это заставляет весь мой компьютер зависать. Даже диспетчер задач перестает работать. Обычно мне приходится перезагружать компьютер, чтобы решить эту проблему.
Ниже представлены мой контроллер и вид на случай, если я пропустил ошибку в кодировке.
Просмотр: hawker_attachment.blade. php
<div class="card-body ">
<div class="tab-content">
<div class="tab-pane active" id="attachment">
<h4>Attachment</h4>
<hr>
<form method="POST" enctype="multipart/form-data" action="{{ route('hawkerInfo_attachment.add',auth()->user()->user_id) }}">
@csrf
<div class="form-group mb-4 col-md-9">
<strong><label for="icpic">Identification Card</label></strong>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" id="icpic" name="icpic" aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" id="icLabel" for="icpic">@if($hawkeradd->ic_pic == null)<small class="text-muted ml-2">Select appropriate file</small> @else {{$hawkeradd->ic_pic}} @endif</label>
</div>
<div class="input-group-append">
<button class="btn btn-primary" type="button" id="inputGroupFileAddon01" data-toggle="modal" data-target="#picModal" data-whatever="Identification Card">Lihat</button>
</div>
</div>
</div>
<div class="form-group mb-4 col-md-9">
<strong><label for="picture">User Picture</label></strong><small class="text-muted ml-2">(Your picture should adhere to passport photo specifications)</small>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" id="picture" name="picture" aria-describedby="inputGroupFileAddon02">
<label class="custom-file-label" id="picLabel" for="picture">@if($hawkeradd->picture == null)<small class="text-muted ml-2">Select appropriate file</small> @else {{$hawkeradd->picture}} @endif</label>
</div>
<div class="input-group-append">
<button class="btn btn-primary" type="button" id="inputGroupFileAddon02" data-toggle="modal" data-target="#picModal" data-whatever="User Picture">Lihat</button>
</div>
</div>
</div>
<div class="form-group mb-4 col-md-9">
<strong><label for="utility_bil">Copy of Utility Bill</label></strong>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" id="utility_bil" name="utility_bil" aria-describedby="inputGroupFileAddon03">
<label class="custom-file-label" id="bilLabel" for="utility_bil">@if($hawkeradd->utility_bil == null)<small class="text-muted ml-2">Select appropriate file</small> @else {{$hawkeradd->utility_bil}} @endif</label>
</div>
<div class="input-group-append">
<button class="btn btn-primary" type="button" id="inputGroupFileAddon03" data-toggle="modal" data-target="#picModal" data-whatever="Copy of Utility Bill">Lihat</button>
</div>
</div>
</div>
<div class="form-group mb-4 col-md-9">
<strong><label for="ty2_inject">Vaccination</label></strong>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" id="ty2_inject" name="ty2_inject" aria-describedby="inputGroupFileAddon04">
<label class="custom-file-label" id="ty2Label" for="ty2_inject">@if($hawkeradd->ty2_inject == null)<small class="text-muted ml-2">Select appropriate file</small> @else {{$hawkeradd->ty2_inject}} @endif</label>
</div>
<div class="input-group-append">
<button class="btn btn-primary" type="button" id="inputGroupFileAddon04" data-toggle="modal" data-target="#picModal" data-whatever="Vaccination">Lihat</button>
</div>
</div>
</div>
<hr>
<div class="text-center alert-warning">Your files should be in the following format: .jpg, .jpeg, or .png only</div>
<hr>
<button type="submit" class="btn btn-primary float-right">@if($hawkeradd->ic_pic != null) Update @else Save @endif</button>
</form>
</div>
</div>
</div>
Контроллер: HawkerAddController
public function stateAdd(Request $request)
{
$hawkerAdd = HawkerAdd::find(auth()->user()->user_id);
if($hawkerAdd->ic_pic != null) {
if ($request->hasFile('icpic')) {
$request->validate([
'icpic' => ['image', 'mimes:jpeg,png,jpg', 'max:2048'],
]);
}
if ($request->hasFile('picture')) {
$request->validate([
'picture' => ['image', 'mimes:jpeg,png,jpg', 'max:2048'],
]);
}
if ($request->hasFile('utility_bil')) {
$request->validate([
'utility_bil' => ['image', 'mimes:jpeg,png,jpg', 'max:2048'],
]);
}
if ($request->hasFile('ty2_inject')) {
$request->validate([
'ty2_inject' => ['image', 'mimes:jpeg,png,jpg', 'max:2048'],
]);
}
}
else {
$request->validate([
'icpic' => ['required', 'image', 'mimes:jpeg,png,jpg', 'max:2048'],
'picture' => ['required', 'image', 'mimes:jpeg,png,jpg', 'max:2048'],
'utility_bil' => ['required', 'image', 'mimes:jpeg,png,jpg', 'max:2048'],
'ty2_inject' => ['required', 'image', 'mimes:jpeg,png,jpg', 'max:2048'],
]);
}
if ($request->hasFile('icpic')) {
$file = $request->file('icpic');
$extension = $file->getClientOriginalExtension();
$icpics = auth()->user()->user_id.'_ic'.'.'.$extension;
$path = $request->file('icpic')->storeAs(
'/hawkers/ic_pics', $icpics, 'public'
);
}
if ($request->hasFile('picture')) {
$file2 = $request->file('picture');
$extension2 = $file2->getClientOriginalExtension();
$userpics = auth()->user()->user_id.'_user'.'.'.$extension2;
$path2 = $request->file('picture')->storeAs(
'/hawkers/user_pics', $userpics, 'public'
);
}
if ($request->hasFile('utility_bil')) {
$file3 = $request->file('utility_bil');
$extension3 = $file3->getClientOriginalExtension();
$billpics = auth()->user()->user_id.'_bil'.'.'.$extension3;
$path3 = $request->file('utility_bil')->storeAs(
'/hawkers/bill_pics', $billpics, 'public'
);
}
if ($request->hasFile('ty2_inject')) {
$file4 = $request->file('ty2_inject');
$extension4 = $file4->getClientOriginalExtension();
$ty2pics = auth()->user()->user_id.'_ty2'.'.'.$extension4;
$path4 = $request->file('ty2_inject')->storeAs(
'/hawkers/ty2_pics', $ty2pics, 'public'
);
}
if ($request->hasFile('icpic')) {
$hawkerAdd->ic_pic = $icpics;
}
if ($request->hasFile('picture')) {
$hawkerAdd->picture = $userpics;
}
if ($request->hasFile('utility_bil')) {
$hawkerAdd->utility_bil = $billpics;
}
if ($request->hasFile('ty2_inject')) {
$hawkerAdd->ty2_inject = $ty2pics;
}
$hawkerAdd->save();
return redirect()->route('hawkerInfo_attachment.view')->with('success',"Attachment successfully stored!");
}