php - Unable to upload image in Laravel 7.x: ”C:xampp mpphpE0A1.tmp“ this error show in my database banner_photo column
Get the solution ↓↓↓I'm trying to upload image in mysql database, but in the banner_photo field "C:\xampp\tmp\phpE0A1.tmp" this .tmp file automatically generated every time. Please help to find what is the problem and what is the solution of it!
my index.blade.php file
@extends('admin.layout.master')
@section('content')
<div class="container-scroller">
<!-- partial:partials/_navbar.html -->
@include('admin.layout.nav')
<!-- partial -->
<div class="container-fluid page-body-wrapper">
<!-- partial:partials/_sidebar.html -->
@include('admin.layout.sidebar')
<!-- partial -->
<div class="main-panel">
<div class="content-wrapper">
<!-- Page Title Header Starts-->
<div class="row page-title-header">
<div class="col-12">
<div class="page-header">
<h4 class="page-title">Dashboard</h4>
</div>
</div>
</div>
<!-- Page Title Header Ends-->
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12 grid-margin">
<div class="card">
<div class="card-body">
<div class="d-flex justify-content-between">
<h4 class="card-title mb-0">Banner</h4>
<a href="#"><small>Show All</small></a>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est quod cupiditate esse fuga</p>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>H4 Title</th>
<th>H2 Title</th>
<th>Paragraph</th>
<th>Image</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach($banners as $row)
<tr>
<td>{{$row->id}}</td>
<td>{{$row->h4_title}}</td>
<td>{{$row->h2_title}}</td>
<td>{{$row->banner_paragraph}}</td>
<td>{{$row->banner_photo}}</td>
<td><button type="submit" class="btn btn-primary"><i class="fas fa-edit"></i>EDIT</button></td>
<td><button type="submit" class="btn btn-danger"><i class="far fa-trash-alt"></i>DELETE</button></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
<!-- partial:partials/_footer.html -->
@include('admin.layout.footer')
<!-- partial -->
</div>
<!-- main-panel ends -->
</div>
<!-- page-body-wrapper ends -->
</div>
<!-- container-scroller -->
@endsection
my web.php file
<?php
use Illuminate\Support\Facades\Route;
/*
|
my BannerController.php file
{-code-3}
N.B: I'm using Laravel 7.x
Answer
Answer
Solution:
When you upload a file, php will assign a temporary name and location (for example:C:\xampp\tmp\phpE0A1.tmp
) until you locate it somewhere.
Usually what you save in the database is the location and the name of the file once it is located in a directory of your project
So, store the file first, and then save the path in the database:
public function store(Request $request)
{
$this->validate($request, [
'h4_title' => 'required',
'h2_title' => 'required',
'banner_paragraph' => 'required',
'banner_photo' => 'required|file'
]);
$path = $request->file('banner_photo')->store('public/image/banner_image');
$banner = new Banner([
'h4_title' => $request->get('h4_title'),
'h2_title' => $request->get('h2_title'),
'banner_paragraph' => $request->get('banner_paragraph'),
'banner_photo' => $path
]);
$banner->save();
return redirect()->back()->with('success', 'Data Added');
}
Or, since you are storing the file onpublic/
storage directory, you may use the public disk to store the file:
$path = $request->file('banner_photo')->store(
'image/banner_image', 'public'
);
Bonus:
You'll can access to it by the url on yourindex.blade
table:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>H4 Title</th>
<th>H2 Title</th>
<th>Paragraph</th>
<th>Image</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach($banners as $row)
<tr>
<td>{{$row->id}}</td>
<td>{{$row->h4_title}}</td>
<td>{{$row->h2_title}}</td>
<td>{{$row->banner_paragraph}}</td>
<td><img src="{{ asset( Storage::url($row->banner_photo) ) }}" /></td>
<td><button type="submit" class="btn btn-primary"><i class="fas fa-edit"></i>EDIT</button></td>
<td><button type="submit" class="btn btn-danger"><i class="far fa-trash-alt"></i>DELETE</button></td>
</tr>
@endforeach
</tbody>
</table>
Note: Furthermore, you should create a symbolic link atpublic/storage
which points to thestorage/app/public
directory.
References:
File Storage File Uploads.
File Storage The Public Disk.
File Storage File URLs.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: too few arguments to function laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.