在laravel 5.3中上传图像时,在null错误时调用成员函数getClientOriginalExtension()

时间:2017-02-27 11:20:18

标签: php laravel-5.3

我正在laravel 5.3中开发一个项目,我必须创建一个1字段表单来更改我的表单的网站徽标。

enter image description here

我不需要在数据库中保存路径。我只需要在\ public \ images \中上传带有name徽标的文件,只允许使用png文件。所以它将是\public\images\logo.png

以下是表单代码。

<div class="col-md-8 col-md-offset-2">

    @if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif

    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <strong>{{ $message }}</strong>
    </div>
    <img src="/images/{{ Session::get('path') }}">
    @endif


    <div class="box box-info">
        <div class="box-header with-border text-center">
            <h3 class="box-title">Basic Info</h3>
        </div>
        <!-- /.box-header -->
        <!-- form start -->
        <form class="form-horizontal" action="{{ url('/') }}/admin/change-site-logo" enctype="multipart/form-data" method="POST">
            <div class="box-body">
                {{ csrf_field() }}
                <div class="form-group">
                    <label for="logo" class="col-sm-3 control-label">Logo</label>

                    <div class="col-sm-9">
                        <input type="file" class="form-control" id="logo" name="logo" placeholder="Logo Image">
                    </div>

                </div>

                <div class="form-group">
                    <label for="logo" class="col-sm-3 control-label"></label>

                    <div class="col-sm-9">
                        <img style="width: 200px; height: 50px;" src="https://www.google.com.pk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
                    </div>

                </div>


            </div>
            <!-- /.box-body -->
            <div class="box-footer">
                <button type="submit" class="btn btn-info pull-right">Save</button>
            </div>
            <!-- /.box-footer -->
        </form>
    </div>
    <br /><br />

</div>

这是路线Route::post('/admin/change-site-logo', 'adminController@logo_change');

和控制器如下

class adminController extends Controller
{

    public function logo_change(Request $request) {

        $this->validate($request, [
            'logo' = > 'required|image|mimes:png|max:2048',
        ]);

        $imageName = 'logo.'.$request->image->getClientOriginalExtension();
        $request->image->move(public_path('images'), $imageName);

        return back()
            ->with('success', 'Image Uploaded Successfully.')
            ->with('in path', $imageName);
    }
}

我第一次写这种代码所以不知道该修复什么。我只知道错误是Call to a member function getClientOriginalExtension() on null

1 个答案:

答案 0 :(得分:0)

尝试更改

$request->image

$request->logo

在您创建的行$imageName