skip to Main Content

hello im trying to upload multiple file on laravel. im following my others single upload file laravel. but when im trying to submit, it says an error

Call to a member function getClientOriginalName() on array

i know the problem, but i dont know how to solved it.
here’s my blade view

<div class="row pb-3">
    <div class="col-sm-4"><label>Akta Lain Lain </label></div>
    <div class="col-sm-8">
        <div class="custom-file" style="width: 500px; height: 40px; cursor: pointer;">
            <input type="file" class="custom-file-input" name="ALL[]" multiple>
            <label class="custom-file-label">Choose file</label>
        </div>
    </div>
</div>

and here’s my controller

$deb = new Debitur_Badan_Usaha();
     if ($request->hasFile('ALL')) {
        $files = [];
           foreach ($request->file('ALL') as $items) {
              if ($items->isValid()) {
                $name_all = time().'_'.rand(1,9999).'_'.$request->ALL->getClientOriginalName();
                $items->move('data_debitur/', $name_all);
                $files [] = $name_all;
               }
            }
                $deb->AKTE_LAIN_LAIN = $files;
                $deb->save();
      }

i think the problem bcs on the name blade file is ALL[] but the getclientoriginalname doesnt accept an array? does anyone know how to solved it so it can be uploading multiple image? thank u

updated controller

$deb = new Debitur_Badan_Usaha();

if ($request->hasFile('ALL')) {
    $files = [];
    foreach ($request->ALL as $file) {
        if ($file->isValid()) {
            $name_all = time().'_'.rand(1,9999).'_'.$file->getClientOriginalName();
            $file->move('data_debitur/', $name_all);
            array_push($files, $name_all);
        }
    }
    $deb->AKTE_LAIN_LAIN = $files;
    $deb->save();
}

2

Answers


  1. You were close, you already have a foreach loop to deal with the array of files, all you need to do is to now utilize it and use it, here is how:

    Note: I changed items -> file, then for getting the original name, you can call that directly on the file.

    if ($request->hasFile('ALL')) {
    
        //foreach file request
        foreach($request->ALL as $file) {
            if ($file->isValid()) {
                //here we will call the file originalname
                $name_all = time().'_'.rand(1,9999).'_'.$file->getClientOriginalName();
                //moving the file
                $file->move('data_debitur/', $name_all);
    
                //we add the file to the field and save it
                $deb->AKTE_LAIN_LAIN = $name_all;
                $deb->save();
            }
        }
    }
    

    Extra: you can also get the extension like $file->getClientOriginalExtension();

    Login or Signup to reply.
  2. Try this one:

     $deb = new Debitur_Badan_Usaha();
            $files = [];
               foreach ($request->file('ALL') as $item) {
                  if ($item->isValid()) {
                    $name = time().'_'.rand(1,9999).'_'.$item->getClientOriginalName();
                    $item->move('data_debitur/', $name);
                    $files [] = $name;
                   }
                }
                    $deb->AKTE_LAIN_LAIN = $files;
                    $deb->save();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search