Laravel 图片上传到数据库

这个函数是我用来存储新公司的函数:


public function store(Request $request)

{   


    $file = $request->file('logo');

    $filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();

    $path = $file->storeAs('public', $filename);

    dd($path);            


    Company::create([

        'name' => $request->name,

        'email' => $request->email,

        'logo' => $request->logo,

        'website' => $request->website

    ]);


    return redirect('/company/all');

}

该视图的形式如下:


@extends('layouts.app')


@section('content')

    <div class="card">

        <div class="card-body">

            <h4 class="card-title">Create a Company</h4>

        </div>


        <div class="container">

            <div class="jumbotron">

                <ul class="list-group">

                    <li class="list-group-item">

                        <h3>Enter Company Information:</h3>

                        <form action="{{ route('company.store') }}" enctype="mutlipart/form-data" method="POST">

                            @csrf

                            <div class="form-group">

                                <input type="text" class="form-control" name="name" placeholder="Company name" value="{{ old('name') }}">

                            </div>

                            <div class="form-group">

                                <input type="text" class="form-control" name="email" placeholder="Email" value="{{ old('email') }}">

                            </div>

                            <div class="form-group">

                                <input class="form-control" type="file" name="logo">

                            </div>



基本上我想要做的是将图像的名称发送到数据库并将图像上传到我的公共文件夹。这段代码两者都没有发生。当我删除从 $file 开始直到 dd($path) 的部分时;它将值添加到数据库,但图像尚未上传。


有什么帮助吗?提前致谢。


凤凰求蛊
浏览 146回答 5
5回答

catspeake

首先,表单上的属性是错误的,enctype="mutlipart/form-data"而且应该是错误的enctype="multipart/form-data"或者您可以根据您的要求使用以下代码:if($request->hasFile('logo')){&nbsp; &nbsp; &nbsp; &nbsp; $file = $request->file('logo');&nbsp; &nbsp; &nbsp; &nbsp; $fileName = 'company-logo-' .time().$file->getClientOriginalName();&nbsp; &nbsp; &nbsp; &nbsp; Storage::put('public/'.$fileName,file_get_contents($file));&nbsp; &nbsp; &nbsp; &nbsp; now you can store the $filename variable in database and image will be uploaded to storage/app/public folder&nbsp; &nbsp; }请添加use Storage到文件顶部并运行php artisan storage:link以在存储文件夹和公共文件夹之间建立符号链接

心有法竹

首先你改变enctype="multipart/form-data"而不是enctype="mutlipart/form-data"你的形式。 然后将此代码放入您的控制器public function store(Request $request){&nbsp; if($request->hasFile('logo')) {&nbsp; &nbsp; $img_ext = $request->file('logo')->getClientOriginalExtension();&nbsp; &nbsp; $filename = 'company-logo-' . time() . '.' . $img_ext;&nbsp; &nbsp; $path = $request->file('logo')->move(public_path(), $filename);//image save public folder&nbsp; }&nbsp; //You should store only filename not path in db&nbsp; Company::create([&nbsp; &nbsp; 'name' => $request->name,&nbsp; &nbsp; 'email' => $request->email,&nbsp; &nbsp; 'logo' => $filename,&nbsp;&nbsp; &nbsp; 'website' => $request->website&nbsp; ]);&nbsp; &nbsp; return redirect('/company/all');}

慕虎7371278

您可以使用文件池。核心库是用普通 JavaScript 编写的,因此可以在任何地方使用。 

收到一只叮咚

尝试更改为:public function store(Request $request){&nbsp; &nbsp;&nbsp; &nbsp; $file = $request->file('logo');&nbsp; &nbsp; $path = '';&nbsp; &nbsp; if($file) {&nbsp; &nbsp; &nbsp; &nbsp; $filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();&nbsp; &nbsp; &nbsp; &nbsp; $path = $file->storeAs('public', $filename);&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; Company::create([&nbsp; &nbsp; &nbsp; &nbsp; 'name' => $request->name,&nbsp; &nbsp; &nbsp; &nbsp; 'email' => $request->email,&nbsp; &nbsp; &nbsp; &nbsp; 'logo' => $path,&nbsp; &nbsp; &nbsp; &nbsp; 'website' => $request->website&nbsp; &nbsp; ]);&nbsp; &nbsp; return redirect('/company/all');}

三国纷争

&nbsp; if (Input::hasFile('logo')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file = Input::file('logo');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ext = $file->getClientOriginalExtension();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file_name = 'company-logo-' . time() . ".{$ext}";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = base_path().'/public/';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file->move($path , $file_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP