Home > Net >  Laravel 8 - Upload multiple files at the same time with different inputs
Laravel 8 - Upload multiple files at the same time with different inputs

Time:01-07

I have many input files with different names, but when I upload only one file is saved, that's the reason why?

_form.blade.php :

<input type="file" name="tampak_depan" />
<input type="file" name="tampak_kiri_depan" />
<input type="file" name="tampak_kanan_depan" />
<input type="file" name="tampak_belakang" />

Controller.php :

tampak depan :

$tampak_depan = $request->file('tampak_depan');
    if($tampak_depan !== null) {
        $art->tampak_depan = time().'_tampak_depan_'.$tampak_depan->getClientOriginalName();
    }
    if($tampak_depan !== null) {
        $path = 'image/asuransi-kendaraan-bermotor/';
        if($tampak_depan->move($path,$art->tampak_depan) == false) {
            print $tampak_depan->getErrorMessage();
            die;
        }
    }

tampak kiri depan :

$tampak_kiri_depan = $request->post('tampak_kiri_depan'); 
    if($tampak_kiri_depan !== null) {
        $art->tampak_kiri_depan = time().'tampak_kiri_depan'.$tampak_kiri_depan->getClientOriginalName();
    }
    if($tampak_kiri_depan !== null) {
        $path = 'image/asuransi-kendaraan-bermotor/';
        if($tampak_kiri_depan->move($path,$art->tampak_kiri_depan) == false) {
            print $tampak_kiri_depan->getErrorMessage();
            die;
        }
    }

tampak kanan depan :

$tampak_kanan_depan = $request->post('tampak_kanan_depan');
    if($tampak_kanan_depan !== null) {
        $art->tampak_kanan_depan = time().'tampak_kanan_depan'.$tampak_kanan_depan->getClientOriginalName();
    }
    if($tampak_kanan_depan !== null) {
        $path = 'image/asuransi-kendaraan-bermotor/';
        if($tampak_kanan_depan->move($path,$art->tampak_kanan_depan) == false) {
            print $tampak_kanan_depan->getErrorMessage();
            die;
        }
    }

tampak belakang :

$tampak_belakang = $request->post('tampak_belakang');
    if($tampak_belakang !== null) {
        $art->tampak_belakang = time().'tampak_belakang'.$tampak_belakang->getClientOriginalName();
    }
    if($tampak_belakang !== null) {
        $path = 'image/asuransi-kendaraan-bermotor/';
        if($tampak_belakang->move($path,$art->tampak_belakang) == false) {
            print $tampak_belakang->getErrorMessage();
            die;
        }
    }

CodePudding user response:

Looks like you're using the right function in the first example, but the wrong function in the others.

In 'tampak depan', you're using $request->file(), but in the others you're using $request->post().

Change those to $request->file() and they should work.

  •  Tags:  
  • Related