Laravel 来自控制器的图像验证不工作
Laravel image validation from controller not working
我正在尝试为我的表单输入创建一个验证器,只接受图像。我在堆栈和其他论坛上搜索过这里,但我得到的所有答案都不适合我。大多数答案都缺少 enctype="multipart/form-data".. 但是显然这里不是这种情况。这是我的表格和我的控制器:
表格:
<form method="post" action="{{url('file')}}" enctype="multipart/form-data">
@csrf
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-success btn-block" type="button" style="width: 100px !important"><i class="fldemo glyphicon glyphicon-plus" ></i>Add</button>
</div>
</div>
<div class="clone hide">
<div class="hdtuto control-group lst input-group" style="margin-top:10px">
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-danger btn-block" type="button" style="width: 100px !important"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>
</div>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
<?php
控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\File;
class FileController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('create');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'filenames' => 'mimetypes:jpeg,png,jpg',
'filenames.*' => 'required'
],$messages = [
'required' => 'The :attribute field is required.',
'mimes' => 'Apenas jpeg, png, png são permitidos.'
]
);
$files = [];
if($request->hasfile('filenames'))
{
foreach($request->file('filenames') as $file)
{
$name = time().rand(1,100).'.'.$file->extension();
$file->move(public_path('files'), $name);
$files[] = $name;
}
}
$file= new File();
$file->filenames = $files;
$file->save();
return back()->with('success', 'Data Your files has been successfully added');
}
}
我尝试将验证更改为:'mimes:jpeg,png |max:4096, required',
,但仍然无法正常工作..
这样使用
$rules = array(
'filenames' => 'required|array',
'filenames.*' => 'image|mimes:jpg,jpeg'
);
我正在尝试为我的表单输入创建一个验证器,只接受图像。我在堆栈和其他论坛上搜索过这里,但我得到的所有答案都不适合我。大多数答案都缺少 enctype="multipart/form-data".. 但是显然这里不是这种情况。这是我的表格和我的控制器:
表格:
<form method="post" action="{{url('file')}}" enctype="multipart/form-data">
@csrf
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-success btn-block" type="button" style="width: 100px !important"><i class="fldemo glyphicon glyphicon-plus" ></i>Add</button>
</div>
</div>
<div class="clone hide">
<div class="hdtuto control-group lst input-group" style="margin-top:10px">
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-danger btn-block" type="button" style="width: 100px !important"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>
</div>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
<?php
控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\File;
class FileController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('create');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'filenames' => 'mimetypes:jpeg,png,jpg',
'filenames.*' => 'required'
],$messages = [
'required' => 'The :attribute field is required.',
'mimes' => 'Apenas jpeg, png, png são permitidos.'
]
);
$files = [];
if($request->hasfile('filenames'))
{
foreach($request->file('filenames') as $file)
{
$name = time().rand(1,100).'.'.$file->extension();
$file->move(public_path('files'), $name);
$files[] = $name;
}
}
$file= new File();
$file->filenames = $files;
$file->save();
return back()->with('success', 'Data Your files has been successfully added');
}
}
我尝试将验证更改为:'mimes:jpeg,png |max:4096, required',
,但仍然无法正常工作..
这样使用
$rules = array(
'filenames' => 'required|array',
'filenames.*' => 'image|mimes:jpg,jpeg'
);