laravel 5.4 上传图片

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

问题描述

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

if ($request->hasFile('input_img')) {if($request->file('input_img')->isValid()) {尝试 {$file = $request->file('input_img');$name = rand(11111, 99999) .'.'.$file->getClientOriginalExtension();$request->file('input_img')->move("fotoupload", $name);} catch (IlluminateFilesystemFileNotFoundException $e) {}}}

图片上传成功但代码抛出异常:

<块引用>

MimeTypeGuesser.php 第 123 行中的 FileNotFoundException

文件是我的代码有问题还是laravel 5.4的bug,谁能帮我解决问题?

我的查看代码:

{{ csrf_field() }}<div class="form-group"><label for="imageInput">文件输入</label><input data-preview="#preview" name="input_img" type="file" id="imageInput"><img class="col-sm-6" id="preview" src=""><p class="help-block">这里是块级帮助文本示例.</p>

<div class="form-group"><label for="">提交</label><input class="form-control" type="submit">

</表单>

解决方案

试试这个代码.这将解决您的问题.

public function fileUpload(Request $request) {$this->validate($request, ['input_img' =>'必需|图像| 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','图片上传成功');}}

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 (IlluminateFilesystemFileNotFoundException $e) {

        }
    }
}

Image was successfully uploaded but the code threw an exception :

FileNotFoundException in MimeTypeGuesser.php line 123

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 ?

My view code:

<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 上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)