0

Hi guys need help in my project how does laravel handle this in the backend?

How about if you have multiple input:

Photo 1: 
<input type="file" name="name">
Photo 2: 
<input type="file" name="name">

    $file_upload = [];
    $("input[name=name]").each(function()){
      $upload_val = $(this).prop('files')[0];
      $file_upload.push($upload_val);
    }


   var data_post = new FormData(); 
   data_post.append('uploads',$file_upload);
   $.ajax({
  url: "app/upload",
  type: "POST",
  processData: false,
  contentType: false,
  data: data_post,
  success:function(res){
          console.log(res)
        }
   });
Arcubalino
  • 141
  • 1
  • 3
  • 14
  • http://laraveldaily.com/upload-multiple-files-laravel-5-4/ hope that helps – Demonyowh May 18 '17 at 10:54
  • Hi @Demonyowh thanks for the link but the example there is multiple selected in one input type file: but in my case i got individual input type file but it is many: – Arcubalino May 18 '17 at 10:59
  • there's no difference since you're appending all the image in the same variable `uploads` so in the backend you should just call it by `$this->input('uploads');` – Demonyowh May 18 '17 at 11:06
  • I'll try that thanks – Arcubalino May 18 '17 at 11:31
  • if you have two inputs with same name in one form, when you submit the form you have only one input in backend. So if you want to upload two or more file you must select different names for every inputs. like `Photo 1: Photo 2: `. – Hakan SONMEZ May 18 '17 at 11:43
  • Hi Hakan my plan in the backend is to explode the appended values which is an array and do a for loop ..so yeah – Arcubalino May 18 '17 at 13:03
  • so just change it from name="name" to name="name[]" and all your problem is solved. But if user does not upload first photo, you have no chance to know this and you assume he uploads only first photo. – Hakan SONMEZ May 18 '17 at 15:04
  • acttually i can create a trappings in my javascript for that.. thx for the suggestion i'll try it. – Arcubalino May 18 '17 at 15:08

1 Answers1

0

I believe you could do something like

Your HTML

<input type="file" value="photos[]">
<input type="file" value="photos[]">

Your backend

$photos = $request::file('photos')
foreach($photos as $photo) {
    // your code
}
morgan9999
  • 731
  • 1
  • 11
  • 30