laravel使用switch case上传文件

时间:2017-07-14 10:30:55

标签: laravel laravel-5 laravel-4 laravel-5.2 laravel-5.3

我怎样才能在不同的文件夹中上传输入文件我需要使用switch case将每个输入文件放在不同的文件夹中并且执行此代码但是当我执行它时没有任何反应我想知道问题在哪里

我的观点

{!! 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;
        }

1 个答案:

答案 0 :(得分:0)

嗯,是的,我曾经做过类似的事情,但之后我会使用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