Laravel使用开关案例上传文件



如何在不同的文件夹中上传输入文件,我需要使用切换大小写将每个输入文件放在不同的文件夹中并执行此代码,但是当我执行它时什么也没发生,我想知道问题在哪里

我的观点

{!! Form::file('file1', null,['class'=>'form-control']) !!}
{!! Form::file('file2', null,['class'=>'form-control']) !!}
{!! Form::file('file3', null,['class'=>'form-control']) !!}
{!! Form::file('file4', null,['class'=>'form-control']) !!}

我的控制器

$model = new Files($request->all());
switch ($model) {
case "file1":
if ($request->hasFile('file1')) {
$file = $request->file('file1');
$destinationPath = public_path() . '/file1';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file1'] = $filename;
$model -> file1 = $filename;
$model->save();
}
break;
case "file2":
if ($request->hasFile('file2')) {
$file = $request->file('file2');
$destinationPath = public_path() . '/file2';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file2'] = $filename;
$model->file2 = $filename;
$model->save();
}
break;
case "file3":
if ($request->hasFile('file3')) {
$file = $request->file('file3');
$destinationPath = public_path() . '/file3';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file3'] = $filename;
$model->file3 = $filename;
$model->save();
}
break;
case "file4":
if ($request->hasFile('file4')) {
$file = $request->file('file4');
$destinationPath = public_path() . '/file4';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file4'] = $filename;
$model->file4 = $filename;
$model->save();
}
break;
}

嗯,是的,我做过这样的事情,但我使用循环foreach然后if

这是我的示例代码,希望它可以帮助:-D

$requests = $request->all();
$model =  new File;
foreach ($requests as $key => $val) {
if ($key == 'file1' && !empty($val)) {
$destinationPath = public_path() . '/file1';
$filename = $val->getClientOriginalName();
$val->move($destinationPath, $filename);
$model -> file1 = $filename;
$model->save();
}
if ($key == 'file2' && !empty($val)) {
$destinationPath = public_path() . '/file2';
$filename = $val->getClientOriginalName();
$val->move($destinationPath, $filename);
$model -> file1 = $filename;
$model->save();
}
// And do it again as much as you need :D
}

来了,希望能帮到你朋友:D

相关内容

  • 没有找到相关文章

最新更新