Laravel 5.3 - Image validation not working

Question

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

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 image flag, everthing just works fine.

I can put things like required or 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:

added 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:

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

}
}

Show source
| laravel   | php   | laravel-5   | laravel-5.3   2017-01-07 17:01 2 Answers

Answers to Laravel 5.3 - Image validation not working ( 2 )

  1. 2017-01-07 18:01

    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.

    Hope this helps you.

  2. 2017-01-08 11:01

    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

Leave a reply to - Laravel 5.3 - Image validation not working

◀ Go back