Laravel上传图片错误

时间:2018-02-16 10:13:00

标签: laravel

错误:图片必须是以下类型的文件:jpeg,png,jpg,gif,svg。

当我尝试从表单上传图片时出现此错误,图片为.jpeg,我认为它应该有效并且图像应该是必需的| mimes:jpeg,png ,JPG,GIF,SVG | MAX:2048'

我的控制器

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

**我的刀片**

<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 个答案:

答案 0 :(得分:2)

您缺少enctype =“multipart / form-data” 你的代码很好。但无论如何,我建议使用image - intervention

答案 1 :(得分:0)

也许你可以这样做:

<!-- 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>

?>

我没有测试过,在手机上写过。