Laravel 5.3 - 图像验证不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41523782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:05:13  来源:igfitidea点击:

Laravel 5.3 - Image validation not working

phplaravellaravel-5laravel-5.3

提问by Luuk Wuijster

I have a problem with the validation of images or mimes overall.

我对图像或 mime 整体的验证有问题。

This is my code:

这是我的代码:

$this->validate($request, [
        'title' => 'required|max:50',
        'content' => 'required|min:20',
        'description' => 'required|max:140',
        'file' => 'image'
    ]);

When I try to upload any file I get the error:

当我尝试上传任何文件时,出现错误:

The file failed to upload.

文件上传失败。

When I dont have the imageflag, everthing just works fine.

当我没有image标志时,一切都很好。

I can put things like requiredor max:5000.

我可以把诸如required或 之类的东西放进去max:5000

I looked at the documentation and it should work, but it doesn't.

我查看了文档,它应该可以工作,但没有。

So what am I doing wrong?

那我做错了什么?

EDIT:

编辑:

Added form:

新增表格:

 {!! Form::open(['method' => 'POST', 'action' => 'PostController@store', 'files' => 'true' ]) !!}

            <div class="form-group">

                {!! Form::label('title', 'Title:') !!}<br>
                {!! Form::text('title', null, ['class' => 'form-control']) !!}
                <small>Max 50 characters</small>

                <br>

                {!! Form::label('description', 'Description:') !!}<br>
                {!! Form::textarea('description', null, ['class' => 'form-control', 'rows' => 2, 'cols' => 50]) !!}
                <small>Max 140 characters</small>

                <br>

                {!! Form::label('content', 'Content:') !!}<br>
                {!! Form::textarea('content', null, ['class' => 'form-control', 'id' =>'content', 'rows' => 8, 'cols' => 50]) !!}

                <br>

                {!! Form::label('file', 'Upload a thumbnail here:') !!} <br>
                {!! Form::file('file', null, ['class' => 'form-control']) !!} <br>
                <small>Only jpeg, png, bmp, gif, or svg</small>

            </div>

                {!! Form::submit(null, ['class' => 'btn btn-primary']) !!}

                {!! Form::close() !!}

EDIT 2:

编辑2:

added html:

添加了 html:

<form method="POST" action="https://blog.luukwuijster.eu" accept-charset="UTF-8" enctype="multipart/form-data"><input name="_token" type="hidden" value="N72xyc8mmbdFGrS78sdhIqh25awN30AboL9ecqGm">

            <div class="form-group">

                <label for="title">Title:</label><br>
                <input class="form-control" name="title" type="text" id="title">
                <small>Max 50 characters</small>

                <br>

                <label for="description">Description:</label><br>
                <textarea class="form-control" rows="2" cols="50" name="description" id="description"></textarea>
                <small>Max 140 characters</small>

                <br>

                <label for="content">Content:</label><br>
                <textarea class="form-control" id="content" rows="8" cols="50" name="content" style="display: none;"></textarea>

                <br>

                <label for="file">Upload a thumbnail here:</label> <br>
                <input name="file" type="file" id="file"> <br>
                <small>Only jpeg, png, bmp, gif, or svg</small>

            </div>

            <input class="btn btn-primary" type="submit">

            </form>

EDIT 3:

编辑 3:

added the controller

添加了控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use App\User;
use Illuminate\Support\Facades\Auth;
use GrahamCampbell\Markdown\Facades\Markdown;

class PostController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */

public function __construct()
{
    $this->middleware('auth')->except('index', 'show');
}

public function index()
{

    $posts = Post::latest()->get();

    return view('welcome', compact('posts'));
}


/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $input = $request->all();

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

    if($file){
        $name = rand(1, 1000000000).'_'.$file->getClientOriginalName();

        $file->move('images', $name);

        $input['thumbnail'] = $name;
    }else{
        $input['thumbnail'] = "No_Image.png";
    }

    //TODO: validatie voor de thumbnails.

    $this->validate($request->all(), [
        'title' => 'required|max:50',
        'content' => 'required|min:20',
        'description' => 'required|max:140',
        'file' => 'image'
    ]);

    Auth::user()->post()->create($input);

    return redirect('/');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    $post = Post::findOrFail($id);
    $content = Markdown::convertToHtml($post->content);

    return view('post', compact('post', 'content'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $post = Auth::user()->post()->findOrFail($id);
    return view('edit', compact('post'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $input = $request->all();

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

    if($file){
        $name = rand(1, 1000000000).'_'.$file->getClientOriginalName();

        $file->move('images', $name);

        $input['thumbnail'] = $name;
    }

    //TODO: validatie voor de thumbnails.

    $this->validate($request, [
        'title' => 'required|max:50',
        'content' => 'required|min:20',
        'description' => 'required|max:140',
        'file' => 'image'
    ]);

    Auth::user()->post()->findOrFail($id)->update($input);

    return redirect('/home');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    Auth::user()->post()->withTrashed()->findOrFail($id)->forceDelete();

    return redirect('/recyclebin');
}

public function restore($id)
{
    Auth::user()->post()->withTrashed()->findOrFail($id)->restore();

    return redirect('/home');
}

public function recyclebin()
{
    $posts = Post::onlyTrashed()->get();

    return view('recyclebin', compact('posts'));
}

public function remove($id){

    //Post::findOrFail($id)->delete();
    Auth::user()->post()->findOrFail($id)->delete();
    return redirect('/home');

}
}

采纳答案by Advaith

Try changing you controller like this --

尝试像这样改变你的控制器——

public function store(Request $request) {
    $this->validate($request, [
        'title' => 'required|max:50',
        'content' => 'required|min:20',
        'description' => 'required|max:140',
        'file' => 'image'
    ]);
    $input = $request->all();

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

    if($request->hasFile('file')){
        $name = rand(1, 1000000000).'_'.$file->getClientOriginalName();

        $file->move('images', $name);

        $input['thumbnail'] = $name;
    }else{
        $input['thumbnail'] = "No_Image.png";
    }

    Auth::user()->post()->create($input);

    return redirect('/');
}

And changing 'files' => 'true'to 'files' => true

并更改'files' => 'true''files' => true

回答by GabMic

In your opening form tag add this:

在您的打开表单标签中添加以下内容:

enctype="multipart/form-data"

and in the file input(where you actually upload it), add this:

并在文件输入(您实际上传的位置)中添加以下内容:

multiple="multiple"

Edit:In every form you should use the csrf_field()method. add also just befor the openning form tag.

编辑:在每种形式中,您都应该使用该csrf_field()方法。在打开表单标签之前也添加。

2019 update: You can add the @csrfdirective instead of csrf_field()method. It's the same, just more convenient for some people.

2019 更新:您可以添加@csrf指令而不是csrf_field()方法。是一样的,只是对某些人来说更方便。

Hope this helps you.

希望这对你有帮助。