Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php &引用;调用数组上的成员函数getClientOriginalName();上载多个文件时出错_Php_Arrays_Laravel_Upload - Fatal编程技术网

Php &引用;调用数组上的成员函数getClientOriginalName();上载多个文件时出错

Php &引用;调用数组上的成员函数getClientOriginalName();上载多个文件时出错,php,arrays,laravel,upload,Php,Arrays,Laravel,Upload,我有一个表单可以接受多个输入,其中一部分是上传。表单接受其他形式的输入,但在尝试上载文件时出错 上传表格的一部分是: {!! Form::label('downloadable_files', 'Downloadable files', ['class' => 'control-label']) !!} {!! Form::file('downloadable_files[]', [ 'multiple', '

我有一个表单可以接受多个输入,其中一部分是上传。表单接受其他形式的输入,但在尝试上载文件时出错

上传表格的一部分是:

{!! Form::label('downloadable_files', 'Downloadable files', ['class' => 'control-label']) !!}
{!! Form::file('downloadable_files[]', [
                    'multiple',
                    'class' => 'form-control file-upload',
                    'data-url' => route('admin.media.upload'),
                    'data-bucket' => 'downloadable_files',
                    'data-filekey' => 'downloadable_files',]) !!}
控制器方法为:

public function store(StoreLessonsRequest $request)
    {
        if (! Gate::allows('lesson_create')) {
            return abort(401);
        }
        $request = $this->saveFiles($request);
        $lesson = Lesson::create($request->all()
        + ['position' => Lesson::where('course_id', $request->course_id)->max('position') + 1]);

        foreach ($request->input('downloadable_files_id', []) as $index => $id) {
        $model = config('laravel-medialibrary.media_model');
        $file = $model::find($id);
        $file->model_id = $lesson->id;
        $file->save();
        }

      return redirect()->route('admin.lessons.index', ['course_id' => $request->course_id]);}
savesFiles()
方法位于FileUploadTraid.php中:

public function saveFiles(Request $request)
{
    if (! file_exists(public_path('uploads'))) {
        mkdir(public_path('uploads'), 0777);
        mkdir(public_path('uploads/thumb'), 0777);
    }

    $finalRequest = $request;

    foreach ($request->all() as $key => $value) {
        if ($request->hasFile($key)) {
            if ($request->has($key . '_max_width') && $request->has($key . '_max_height')) {
                // Check file width
                $filename = time() . '-' . $request->file($key)->getClientOriginalName();
                $file     = $request->file($key);
                $image    = Image::make($file);
                if (! file_exists(public_path('uploads/thumb'))) {
                    mkdir(public_path('uploads/thumb'), 0777, true);
                }
                Image::make($file)->resize(50, 50)->save(public_path('uploads/thumb') . '/' . $filename);
                $width  = $image->width();
                $height = $image->height();
                if ($width > $request->{$key . '_max_width'} && $height > $request->{$key . '_max_height'}) {
                    $image->resize($request->{$key . '_max_width'}, $request->{$key . '_max_height'});
                } elseif ($width > $request->{$key . '_max_width'}) {
                    $image->resize($request->{$key . '_max_width'}, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                } elseif ($height > $request->{$key . '_max_width'}) {
                    $image->resize(null, $request->{$key . '_max_height'}, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                }
                Image::make($file)->resize(320, 150)->save(public_path('uploads') . '/' . $filename);
                $finalRequest = new Request(array_merge($finalRequest->all(), [$key => $filename]));
            } else {
                ***$filename = time() . '-' . $request->file($key)->getClientOriginalName();***
                $request->file($key)->move(public_path('uploads'), $filename);
                $finalRequest = new Request(array_merge($finalRequest->all(), [$key => $filename]));
            }
        }
    }

    return $finalRequest;
}
}

表单中有一部分接受单次上传,但用于多次上传的部分似乎不起作用。我不断发现错误:

Call to a member function getClientOriginalName() on array

in FileUploadTrait.php line 50
at LessonsController->saveFiles(object(StoreLessonsRequest))
in LessonsController.php line 156
at LessonsController->update(object(StoreLessonsRequest), '1')
更新:FileUploadTrait.php中出现错误的行是粗体(或带星号)

-in your case you are not giving  only the name of the file but all the 
  inforamtions of the file make a dd($request->file($key)) and search for the 
  your filename for example : ex.pnj and access it
这是我的代码:在我的控制器中

public function uploadFiles(Request $request){
  $request->validate([
        'files'=>'required',
        'files.*'=>'image|mimes:jpeg,png,jpg,svg|max:2048'
  ]);
          $files = $request->file('files');

        foreach ($files as $file) {
            $name = time(). $file->getClientOriginalName();
            $file->move('gallery',$name);
            Photo::create([
                'name'=>$name,
                'user_id'=>auth()->user()->id
            ]);
        }
        flashy()->success('le contenu a bien été ajouté.');
        return back();
    }
在我的html文件中

<form action="{{route('upload')}}" method="POST" 
      enctype="multipart/form-data">
    @csrf
    <input
            type="file"
            multiple
            id="btn-gallery"
            name="files[]"
            label="Déposez les fichiers ici ou cliquez pour les télécharger.."
            help="Téléchargez les fichiers ici et ils ne seront pas envoyés immédiatement"
            is="drop-files"
    />
    <button type="submit" id="btn-upload" class="btn btn-primary mt-1">télécharger</button>
</form>

@csrf
télécharger
在您的存储方法中

 $files=$request->file('your input name');
 
 $request = $this->saveFiles($files)
在你的职能中:

public function saveFiles($files){
  foreach ($files as $file) {
        $name = time(). $file->getClientOriginalName();
        $file->move('yourFolderStorageName',$name);

}

}

如果你检查
$request
你会发现
可下载的\u文件
是一个文件数组…你需要循环这个来保存你的图像。@James.是的,我做了一个
dd($request->all())
这是一个数组。我需要在哪里循环这个?错误似乎是我把错误的类型传递给了savesFiles()方法在
$request=$this->saveFiles($request);
您需要是动态的,还是可以将文件传递给saveFiles?
$this->saveFiles($request->input('downloadable_files'))
@James我不确定我是否理解dynamism。我在哪里可以尝试传递它?谢谢你的回答。但是,有没有办法修复原始代码。使用你的回答意味着,我必须创建一个新表单,这对我来说现在是相当令人畏惧的。
dd($request->file($key))
给了我
未定义变量:key
的错误。这行
$key
上的
$filename=time().-'.$request->file($key)->getClientOriginalName();