我通过邮递员将多个文件发送到我的服务器并且这些文件已正确上传,但在上传之前未正确验证]
这是我的控制器
public function store(Request $request)
{
// دریافت دایرکتوری مطالبه مربوطه : $demand=Demand::find(72)->files->first()->file_directoryس
//{"title":"this is test title","demandContent":"this is test content "} send as form-data request
//------------------------------------------- Valid Uploaded File ---------------------------------
$rules = array(
'file' => 'required',
'file.' => 'mimes:doc,pdf,docx,zip,jpg,jpeg,rar'
);
$error = Validator::make($request->all(), $rules);
if($error->fails())
return response()->json(['errors' => $error->errors()->all()]);
//-------------------------------------------- Valid Uploaded File -------------------------------
$request->data=json_decode($request->data); //دریافت به صورت جیسون و تبدیل به شی
$demand=new Demand(['title' => $request->data->title,'content'=>$request->data->demandContent,'user_id'=>auth('api')->user()->id]);
if($demand->save()) //اگر درخواست در دیتابیس قبت شد
{
//----------------------------File Upload Scope---------------------------------------
if($request->hasfile('file'))
{
$path='public/demands/'.$demand->id.'/files';
foreach($request->file('file') as $file)
{
$filename=$file->getClientOriginalName();
$file->move($path, $filename);
}
$demand->files()->save(new File(['file_directory'=>$path]));
}
//----------------------------File Upload Scope---------------------------------------
return response()->json(['demand'=>new DemandResource($demand)],200);
}
return response()->json(['state'=>'false']);
}
在您的验证规则中,您忘记了*
,例如:
$rules = [
'file' => 'required',
'file.*' => 'required|file|mimes:doc,pdf,docx,zip,jpg,jpeg,rar',
];