无法在 Laravel 7.x 中上传图片:“C:\xampp\tmp\phpE0A1.tmp” 此错误显示在我的数据库banner_photo 列中

Posted

技术标签:

【中文标题】无法在 Laravel 7.x 中上传图片:“C:\\xampp\\tmp\\phpE0A1.tmp” 此错误显示在我的数据库banner_photo 列中【英文标题】:Unable to upload image in Laravel 7.x: ”C:\xampp\tmp\phpE0A1.tmp“ this error show in my database banner_photo column无法在 Laravel 7.x 中上传图片:“C:\xampp\tmp\phpE0A1.tmp” 此错误显示在我的数据库banner_photo 列中 【发布时间】:2020-09-09 11:12:28 【问题描述】:

我正在尝试将图像上传到 mysql 数据库中,但在banner_photo 字段“C:\xampp\tmp\phpE0A1.tmp”中,这个 .tmp 文件每次都会自动生成。请帮忙看看是什么问题,解决方法是什么!

我的 index.blade.php 文件

@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

我的 web.php 文件

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () 
    return view('index');
);

Route::get('/contact-us', function () 
    return view('contactus');
);

Route::get('/tours', function () 
    return view('tours');
);

// Admin panel Pages Routes

Route::get('/admin', function () 
    return view('admin/index');
);

Route::get('/admin/bannercustomize', function () 
    return view('admin/layout/bannercustomize');
);


// Controller routes

Route::post('store/banner','BannerController@store')->name('store.banner');

Route::get('/admin','BannerController@index')->name('admin.index');

我的 BannerController.php 文件

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Banner;
use Image;
use Illuminate\Support\Str;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

class BannerController extends Controller

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    


        $banners = Banner::all();
        return view('admin.index',compact('banners'));
    


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    
        return view('admin.layout.bannercustomize');
    

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */


    public function store(Request $request)
    
        $this->validate($request, [
            'h4_title' => 'required',
            'h2_title' => 'required',
            'banner_paragraph' => 'required',
            'banner_photo' => 'required'


        ]);

        $banner = new Banner([
            'h4_title' => $request->get('h4_title'),
            'h2_title' => $request->get('h2_title'),
            'banner_paragraph' => $request->get('banner_paragraph'),
            'banner_photo' => $request->file('banner_photo')

        ]);

        $request->banner_photo->store('public/image/banner_image/');

        $banner->save();

        return redirect()->back()->with('success', 'Data Added');
    

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    
        //
    

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    
        //
    

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    
        //
    

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    
        //
    

注意:我使用的是 Laravel 7.x

【问题讨论】:

【参考方案1】:

当您上传文件时,php 会分配一个临时名称和位置(例如:C:\xampp\tmp\phpE0A1.tmp),直到您在某处找到它。

通常你保存在数据库中的是文件的位置和名称,一旦它位于你的项目目录中

所以,先把文件存起来,再把路径存入数据库:

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');

或者,由于您将文件存储在public/存储目录中,您可以使用公共磁盘来存储文件:

$path = $request->file('banner_photo')->store(
    'image/banner_image', 'public'
);

奖金

您可以通过index.blade 表上的网址访问它:

<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>

注意:此外,您应该在指向storage/app/public 目录的public/storage 处create a symbolic link。

参考资料:

文件存储File Uploads。

文件存储The Public Disk。

文件存储File URLs。

【讨论】:

以上是关于无法在 Laravel 7.x 中上传图片:“C:\xampp\tmp\phpE0A1.tmp” 此错误显示在我的数据库banner_photo 列中的主要内容,如果未能解决你的问题,请参考以下文章

在Laravel 5.8中无法上传图片?

部署的laravel网站无法上传图片

laravel上的图片上传无法在线使用

Laravel Vue 多张图片上传失败,每个图片都有一个 NULL 值

laravel 8中多张图片上传的问题

unisharp laravel filemanager 无法上传图片,“请刷新再试一次