dongzhan5286 2015-09-18 09:06
浏览 109
已采纳

Laravel 5验证失败(字段未设置)

Hi im currently coding a gallery in laravel 5 where you can upload images and videos in separted categorys and i got a pretty strange problem with the valdidation method.

The image upload itself works fine and does exactly what it should. But if i try to upload videos (mp4,webm) my Validation always fails with the error, that the file field is required. But if i try the same with a wrong mime type file all works like intended and i the validation method says its the wrong mime type.

The view looks the following:

@extends('templates.base')
@section('content')
@parent
{!! Form::open(['url' => '/gallery/upload/' .$gallery->id, 'files' => true]) !!}
<div>
    {!! $error !!}
</div>
<table>
    <tr>
        <td>
            Title:
        </td>
        <td>
            {!! Form::text('title',old('title'),['class' => 'form_text', 'min' => '3', 'max' => '255']) !!}
        </td>
    </tr>
    <tr>
        <td>
            Description:
        </td>
        <td>
            {!! Form::textarea('description') !!}
        </td>
    </tr>
    <tr>
        <td>
            File:
        </td>
        <td>
            <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
            {!! Form::file('file') !!}
        </td>
    </tr>
    <tr>
        <td>
            External:
        </td>
        <td>
            <select name="external">
                <option value="0" default>No</option>
                <option value="1">Yes</option>}
            </select>
        </td>
    </tr>
    <tr>
        <td>{!! Form::submit(trans('strings.create'),['class' => 'form_submit news']) !!}</td>
    </tr>
</table>
{!! Form::close() !!}
@stop

The controller looks something like this:

if (config('gallery.allowUploads')) {
        if (config('gallery.allowUserUploads')) {
            $gallery = Gallery::find($request->route('id'));

            if (is_null($request->route('id')) || count($gallery) <= 0) {
                return redirect()->back();
            }

            if ($request->isMethod('post')) {
                if ($gallery->type == 'i') {
                    $Validator = Validator::make($request->all(), [
                        'title'       => 'required|min:3|max:255',
                        'description' => 'max:10000',
                        'file'        => 'required|mimes:jpg,jpeg,bmp,png,gif',
                        'external'    => 'required|in:0,1',
                    ]);
                } elseif ($gallery->type == 'v') {
                    $Validator = Validator::make($request->all(), [
                        'title'       => 'required|min:3|max:255',
                        'description' => 'max:10000',
                        'file'        => 'required|mimes:mp4,webm',
                        'external'    => 'required|in:0,1',
                    ]);
                } else {
                    $Validator = Validator::make($request->all(), [
                        'title'       => 'required|min:3|max:255',
                        'description' => 'max:10000',
                        'file'        => 'required|mimes:zip,rar,7z',
                        'external'    => 'required|in:0,1',
                    ]);
                }
                $error = "";

                if ($Validator->fails()) {
                    foreach ($Validator->messages()->All() as $m) {
                        $error .= $m . "</br>";
                    }
                    return view('gallery/items/create', ['gallery' => $gallery, 'error' => $error);
                }

When im uploading a webm

dd($request->file('file'),$gallery->type);

returns

UploadedFile {#29 ▼
   -test: false
   -originalName: "big-buck-bunny_trailer.webm"
   -mimeType: "application/octet-stream"
   -size: 0
   -error: 1
   }
   "v"

But the Validation Method still says it fails because the file field is required.

I just feel like i'm overlooking something. Thanks for your help.

edit:

i just found that when im uploading an image file i get

UploadedFile {#29 ▼
   -test: false
   -originalName: "a8b5KQQ_460s_v1.jpg"
   -mimeType: "image/jpeg"
   -size: 53991
   -error: 0
}
"v"

that error is 0 and on the webm file its 1 but i'm not sure what it means.

  • 写回答

1条回答 默认 最新

  • doufu8588 2015-09-18 13:28
    关注

    From the output it looks like the file is empty and for some reason an mime type cannot be found. If you delve into symfony\Httpfoundation\File\UploadedFile you will see the constructor has the following assignment:

    $this->mimeType = $mimeType ?: 'application/octet-stream';

    Which seems to be why you are getting application/octet-stream as your Mime Type.

    Because Laravels request->file('file'); returns an instance of symfony\Httpfoundation\File\UploadedFile - which performs some checks on the file when the object is constructed - I would guess that whatever error is detected causes Laravel to assume that no file has been uploaded and therefore doesn't perform the validation.

    Either way, the file doesn't make it on to your server, which is good. Incidentally, you should be able to call $request->file('file')->getErrorMessage(); which should tell you what it thinks the problem was.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)