2
votes

Error: The image must be a file of type: jpeg, png, jpg, gif, svg.

I have this error when i try to upload image from the form, the image is .jpeg and i think it should work beacuse 'image'should be 'required|mimes:jpeg,png,jpg,gif,svg|max:2048'

My Controller

public function store(){

    $this->validate(request(),[

        'title' => 'required',

        'image' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',

        'body' => 'required',

    ]);

    auth()->user()->publish(

        new Post(request(['title','image','body']))
    );


    session()->flash('message', 'your post has now been published');


    return redirect('/');
}

** My BLADE**

<form method="POST" action="/posts">

    {{csrf_field()}}

    <div class="form-group">

        <label for="title">Titolo</label>

        <input type="text" class="form-control" id="title" name="title">

    </div>


    <div class="form-group">

        <label for="image">Immagine</label>

        <input type="file" class="form-control" id="image" name="image">

    </div>

    <div class="form-group">

        <label for="body">Corpo</label>

        <textarea id="body" name="body" class="form-control"></textarea>

    </div>

    <div class="form-group">

        <button type="submit" class="bottone">Invia</button>

    </div>

    @include ('layouts.errors')

</form>
2
is this actual error message?Aleksei Maide
form type must be 'enctype' => 'multipart/form-data'arun
@arunkumar ok now it works but none file uploaded into dbMatteo
laravel.com/docs/5.6/filesystem#storing-files refer this to upload filesarun
@arunkumar i have added new route Storage::putFile('photos', new File('img')); but if i try to open the homepage i got The file "img" does not existMatteo

2 Answers

6
votes

You are missing enctype="multipart/form-data" Your codes are fine. But anyway I recommend to use image - intervention.

0
votes

Maybe you could do something like this:

<!-- Controller -->

<?php
public function store(Request $request) {

    $this->validate($request, [
        'title' =>  'required',
        'image' =>  'image' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'body'  =>  'required'
    ]);

    auth()->user()->publish(
        Storage::putFile('photos', new File($request['image']), 'public');
        new Post($request['title', 'image', 'body']);
    );

    session()->flash('message', 'your post has now been published');

    return redirect('/');
}

?>

<!-- routes -->

<?php

Route::post('/posts', controllerName::store());

?>

<!-- Blade -->
<?

     <form method="POST" action="/posts" enctype="multipart/form-data">

           {{csrf_field()}}

           <div class="form-group">

             <label for="title">Titolo</label>

           <input type="text" class="form-control" id="title" name="title">

           </div>


      <div class="form-group">

  <label for="image">Immagine</label>

  <input type="file" class="form-control" id="image" name="image">

</div>

<div class="form-group">

  <label for="body">Corpo</label>

  <textarea id="body" name="body" class="form-control"></textarea>

</div>

<div class="form-group">

  <button type="submit" class="bottone">Invia</button>

</div>

  @include ('layouts.errors')

        </form>

?>

I haven't tested it, wrote it on my phone.