图片上传=> LogicException无法猜测mime类型,因为没有猜测器可用(你启用了php_fileinfo扩展吗?)

时间:2018-03-18 13:29:45

标签: laravel laravel-5

我正在按照以下教程 - > http://laraveldaily.com/upload-multiple-files-laravel-5-4。 当我发布我的广告时,添加内容没有照片。我的错误信息是

  

无法猜测mime类型,因为没有可用的猜测器(你启用了php_fileinfo扩展吗?)

我启用了php_fileinfo并重新启动了服务器,但是知道了。关于如何解决这个问题的任何其他想法?

    public function store(Request $request){



    $Advert = PropertyAdvert::create([
        "address"     => $request->address,
        "county"      => $request->county,
        "town"        => $request->town,
        "type"        => $request->type,
        "rent"        => $request->rent,
        "date"        => $request->date,
        "bedrooms"    => $request->bedrooms,
        "bathrooms"   => $request->bathrooms,
        "furnished"   => $request->furnished,
        "description" => $request->description,
        "user_id" => Auth::id(),
    ]);


    foreach ($request->photo as $photo) {
            $filename = $photo->store('photo');
            PropertyAdvertPhoto::create([
                'property_id' => $Advert->id,
                'filename' => $filename
            ]);
          }

    $id = $Advert->id;

    return redirect("/property/$id");
  }

UploadRequest“请求文件”

这是存储所有验证规则的地方。     

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UploadRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
          return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
      $rules = [
              'address' => 'required'
          ];
          $photos = count($this->input('photo'));
          foreach(range(0, $photos) as $index) {
              $rules['photo.' . $index] = 'min:100';
          }

          return $rules;

    }


}

上传表格

 <form method="POST" action="/property" enctype="multipart/form-data">
                        {{ csrf_field() }}

                          <div class="form-group row">
                              <label for="photo" class="col-md-3 col-form-label text-md-right">Images</label>
                              <div class="col-md-9">
                                <input required type="file" class="form-control" name="photo[]" multiple>
                              </div>
                            </div>

2 个答案:

答案 0 :(得分:0)

您的代码看起来不错,我总是使用Casting between arrays with incompatible element types is not supported: Invalid cast from... ,但您的循环问题是您需要使用foreach方法调用fileinput

file

PS: foreach( $request->file('photo') as $photo )$request->photo之间存在巨大差异所以要猜测mime类型,您应该使用$request->file('photo')方法。

干杯。

答案 1 :(得分:0)

问题来自验证!

如果您有这样的事情:

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

然后将验证码替换为:

        'image' => 'required|image|max:2048',

在我的情况下,我有进一步的更新,如:

'image' => 'required|image|dimensions:min_width=300,min_height=400,ratio=3/4|max:2048',

希望这会有所帮助。不幸的是我没有找到原因 - 为什么Laravel无法检索MIME类型并验证上传的图像文件!这可能是因为另一个缺少的PHP扩展。

doc here找到所有可能的验证案例。