Input::file('filename') returning null Laravel 5.1


Input::file('filename') returning null Laravel 5.1

下面是我的代码:

{!! Form::open(['route' => 'add_auto', 'class'=>'form-horizontal', 'files'=>true, 'method'=>'GET']) !!}
   {!! Form::file('file1'); !!}
   {!! Form::submit('Click Me!'); !!}
{!! Form::close()!!}

And in controller:

if ($request->hasFile('file1')) {
   $file1 = $request->file('file1');
   $request->file('file1')->move('../public/img');
}else{
        echo 'Has not any file!';
}

我不能检索并保存在目录这个文件,因为它说没有文件,并返回null。但是如果我们写入controller:

dd($request->all());

然后显示:

array:1 [▼
"file1" => "list.txt"
]

问题在哪里?!!

提交类型为GETFORM元素无法传输文件!它必须是POST,加上,你必须将enctype属性设置为multipart/form-data

问题是您调用了两次方法"file"。在您的控制器中,只需输入以下内容:

if ($request->hasFile('file1')) {
    $file1 = $request->file('file1');
    $file1->move('../public/img');
} else {
    echo 'Has not any file!';
}