0

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

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>
                            <div class="form-group">
                                <input class="form-control" type="url" name="website" placeholder="Website" value="{{ old('website') }}">
                            </div>
                            <button class="btn btn-primary" type="submit">Add Company</button>
                        </form>
                    </li>
                </ul>
            </div>
        </div>
    </div>
@endsection

这是路线:

Route::post('/add', 'CompaniesController@store')->name('store');

好吧,当我尝试提交此表单时会发生什么,$file 变量总是返回 null:

Error
Call to a member function getClientOriginalExtension() on null
http://localhost:8000/company/add

基本上我想要做的是将图像的名称发送到数据库并将图像上传到我的公共文件夹。这段代码也不会发生。当我擦除从 $file 到 dd($path) 的部分时;它将值添加到数据库中,但尚未上传图像。

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

4

5 回答 5

0

尝试更改为:

public function store(Request $request)
{   

    $file = $request->file('logo');
    $path = '';
    if($file) {
        $filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
        $path = $file->storeAs('public', $filename);
    }            

    Company::create([
        'name' => $request->name,
        'email' => $request->email,
        'logo' => $path,
        'website' => $request->website
    ]);

    return redirect('/company/all');
}
于 2020-08-18T13:41:11.060 回答
0
  if (Input::hasFile('logo')) {
                $file = Input::file('logo');
                $ext = $file->getClientOriginalExtension();
                $file_name = 'company-logo-' . time() . ".{$ext}";
                $path = base_path().'/public/';
                $file->move($path , $file_name);
            }
于 2020-08-18T13:54:37.570 回答
0

首先表单上的属性是错误的,enctype="mutlipart/form-data"应该是错误的enctype="multipart/form-data"

或者您可以根据您的要求使用以下代码:

if($request->hasFile('logo')){
        $file = $request->file('logo');
        $fileName = 'company-logo-' .time().$file->getClientOriginalName();
        Storage::put('public/'.$fileName,file_get_contents($file));
        now you can store the $filename variable in database and image will be uploaded to storage/app/public folder
    }

use Storage在文件顶部添加并运行php artisan storage:link以在存储文件夹和公用文件夹之间建立符号链接

于 2020-08-18T14:01:09.817 回答
0

首先你改变enctype="multipart/form-data"而不是enctype="mutlipart/form-data"你的形式。 然后将此代码放入您的控制器

public function store(Request $request)
{
  if($request->hasFile('logo')) {
    $img_ext = $request->file('logo')->getClientOriginalExtension();
    $filename = 'company-logo-' . time() . '.' . $img_ext;
    $path = $request->file('logo')->move(public_path(), $filename);//image save public folder
  }
  //You should store only filename not path in db
  Company::create([
    'name' => $request->name,
    'email' => $request->email,
    'logo' => $filename, 
    'website' => $request->website
  ]);

    return redirect('/company/all');
}
于 2020-08-18T14:48:16.643 回答
0

您可以使用文件池。核心库是用 vanilla JavaScript 编写的,因此可以在任何地方使用。 访问: https ://pqina.nl/filepond/

于 2021-04-05T11:33:47.630 回答