Laravel 5.3 - 图像验证不起作用

时间:2017-01-07 16:38:40

标签: php laravel laravel-5 laravel-5.3

我对整体图像或哑剧的验证存在问题。

这是我的代码:

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

当我尝试上传任何文件时,我收到错误:

  

文件无法上传。

当我没有image标志时,everthing就可以了。

我可以添加requiredmax:5000等内容。

我查看了文档,它应该可以工作,但事实并非如此。

那么我做错了什么?

编辑:

添加表格:

 {!! 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() !!}

编辑2:

添加了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>

编辑3:

添加了控制器

<?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');

}
}

3 个答案:

答案 0 :(得分:4)

在您的初始表单标记中添加以下内容:

SELECT 
    `main`.*, `ac`.`account_id`
FROM
    `list` AS `main`
        LEFT JOIN
    `account` AS `ac` ON ac.list_id = main.id
WHERE
    (ac.`account_id` = '31')
        AND (`type` IN ('simple' , 'complete'))

并在文件输入中(实际上传它),添加:

enctype="multipart/form-data"

修改 在每种形式中,您都应该使用multiple="multiple" 方法。添加也只是打开表单标记。

希望这会对你有所帮助。

答案 1 :(得分:2)

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

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('/');
}

'files' => 'true'更改为'files' => true

答案 2 :(得分:0)

这是我的代码

$validator = Validator::make($input, [  
   'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
])
if ($validator - > fails()) {
   return $this - > sendError('Validation Error.', $validator - > errors());
}