laravel 5.4上传图片 [英] laravel 5.4 upload image

查看:241
本文介绍了laravel 5.4上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel 5.4中上传文件的控制器代码:

My controller code for upload file in laravel 5.4:

if ($request->hasFile('input_img')) {
    if($request->file('input_img')->isValid()) {
        try {
            $file = $request->file('input_img');
            $name = rand(11111, 99999) . '.' . $file->getClientOriginalExtension();
            $request->file('input_img')->move("fotoupload", $name);
        } catch (Illuminate\Filesystem\FileNotFoundException $e) {

        }
    }
}

图像已成功上传,但是代码引发了异常:

Image was successfully uploaded but the code threw an exception :

MimeTypeGuesser.php第123行中的

FileNotFoundException

FileNotFoundException in MimeTypeGuesser.php line 123

该文件在我的代码中有任何错误,或者是laravel 5.4中的错误,有人可以帮助我解决该问题吗?

The file is there any fault in my code or is it a bug in laravel 5.4, can anyone help me solve the problem ?

我的查看代码:

<form enctype="multipart/form-data" method="post" action="{{url('admin/post/insert')}}">
    {{ csrf_field() }}
    <div class="form-group">
        <label for="imageInput">File input</label>
        <input data-preview="#preview" name="input_img" type="file" id="imageInput">
        <img class="col-sm-6" id="preview"  src="">
        <p class="help-block">Example block-level help text here.</p>
    </div>
    <div class="form-group">
        <label for="">submit</label>
        <input class="form-control" type="submit">
    </div>
</form>

推荐答案

尝试此代码.这样可以解决您的问题.

Try this code. This will solve your problem.

public function fileUpload(Request $request) {
    $this->validate($request, [
        'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    if ($request->hasFile('input_img')) {
        $image = $request->file('input_img');
        $name = time().'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);
        $this->save();

        return back()->with('success','Image Upload successfully');
    }
}

这篇关于laravel 5.4上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆