webentwicklung-frage-antwort-db.com.de

Laravel 5.3 mehrere Datei-Uploads

Wie kann ich mehrere Dateien in Laravel 5.3 hochladen. Wenn ich es mit einem Bild versuche, funktioniert es, aber mehrere Bilder werden nicht hochgeladen.

Das ist mein Code:

 if($request->hasFile('attachment'))
 {
    foreach ($request->allFiles('attachments') as $file) {
        $file->store('users/' . $user->id . '/messages');
    }
 }
10
Jamie

Das funktioniert jetzt so:

$files = $request->file('attachment');

if($request->hasFile('attachment'))
{
    foreach ($files as $file) {
        $file->store('users/' . $this->user->id . '/messages');
    }
}

Ich musste [] nach dem Wert des name-Attributs anhängen, also:

<input type="file" name="attachment[]" multiple>
22
Jamie

Versuchen Sie etwas wie dieses:

public function multiple_upload() {
    // getting all of the post data
    $files = Input::file('images');

    // Making counting of uploaded images
    $file_count = count($files);

    // start count how many uploaded
    $uploadcount = 0;

    foreach($files as $file) {
        $rules = array('file' => 'required');

        //'required|mimes:png,gif,jpeg,txt,pdf,doc'

        $validator = Validator::make(array('file'=> $file), $rules);

        if($validator->passes()){
             $destinationPath = 'uploads';
             $filename = $file->getClientOriginalName();
             $upload_success = $file->move($destinationPath, $filename);
             $uploadcount ++;
         }
    }
}
1
Mayank Pandeyz

Wenn Sie weiterhin mit Blade arbeiten möchten, können Sie Folgendes verwenden:

{{ Form::open(array('url' => 'upload', 'files'=>true)); }}
{{ Form::file('gallery[]', array('multiple'=>true,'accept'=>'image/*'));  }}
{{ Form::submit();  }}
{{ Form::close();  }}

Und in Ihrem Controller:

files = $request->file('gallery');

if($request->hasFile('gallery'))
{
    foreach ($files as $file) {
       // $file->store('users/' . $this->user->id . '/messages');
        //dump($file);
    }
}
0
Malki Mohamed

Gelöst hat es mit diesem einen einfacheren Ansatz. Stellen Sie einfach sicher, dass Ihr Eingabedateityp wie folgt lautet <input type="file" name="images[]" multiple>

        $i = 0;

        foreach($request->file('images') as $file){

            $photo = new Photo;

            // name it differently by time and count
            $imageName = time() . $i . '.' . $file->getClientOriginalExtension();

            // move the file to desired folder
            $file->move('folderName/', $imageName);

            // assign the location of folder to the model
            $photo->image = 'folderName/' . $imageName;

            $photo->status = 1;
            $photo->save();

            $i++;

        } 
0
Edeeson Opina

Controller.Php/Laravel 5.7.28

   $files = $request->file('product_image_id');

   if($request->hasFile('product_image_id')){

       foreach ($files as $file){

          $name = time(). $file->getClientOriginalName();    

          $file->move('images',$name);             

         $productImage = ProductImage::create(['image'=>$name]);    

               $input ['product_image_id'] = $productImage->id;     

           }
0

app/Http/Controller/FileController.php

public function store(Request $request)
{
    $this->validate($request, [

            'filenames' => 'required',

            'filenames.*' => 'mimes:doc,pdf,docx,Zip'

    ]);

    if($request->hasfile('filenames'))
     {
        foreach($request->file('filenames') as $file)
        {
            $name=$file->getClientOriginalName();
            $file->move(public_path().'/files/', $name);  
            $data[] = $name;  
        }
     }

     $file= new File();
     $file->filenames=json_encode($data);
     $file->save();
    return back()->with('success', 'Data Your files has been successfully added');

}

Ansichtsdatei erstellen

resources/views/create.blade.php

<html lang="en">
<head>
  <title>Laravel 5.3 Multiple File Upload Example</title>
  <script src="jquery/1.9.1/jquery.js"></script>
  <link rel="stylesheet" href="3.3.6/css/bootstrap.min.css">
</head>
<body>

<div class="container lst">
@if (count($errors) > 0)
<div class="alert alert-danger">
    <strong>Sorry!</strong> There were more problems with your HTML input.<br><br>
    <ul>
      @foreach ($errors->all() as $error)
          <li>{{ $error }}</li>
      @endforeach
    </ul>
</div>
@endif

@if(session('success'))
<div class="alert alert-success">
  {{ session('success') }}
</div> 
@endif
<h3 class="well">Laravel 5.6 Multiple File Upload</h3>
<form method="post" action="{{url('file')}}" enctype="multipart/form-data">

  {{csrf_field()}}

    <div class="input-group flcustom control-group lst increment" >
      <input type="file" name="filenames[]" class="myfrm form-control">
      <div class="input-group-btn"> 
        <button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
      </div>
    </div>

    <div class="clone hide">
      <div class="flcustom control-group lst input-group" style="margin-top:10px">
        <input type="file" name="filenames[]" class="myfrm form-control">
        <div class="input-group-btn"> 
          <button class="btn btn-danger" type="button"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>
        </div>

      </div>

    </div>
    <button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>        
</div>
<script type="text/javascript">
    $(document).ready(function() {
      $(".btn-success").click(function(){ 
          var lsthmtl = $(".clone").html();
          $(".increment").after(lsthmtl);
      });
      $("body").on("click",".btn-danger",function(){ 
          $(this).parents(".flcustom control-group lst").remove();
      });
    });
</script>
</body>
</html>

Laravel 5.3 - Upload mehrerer Dateien mit Validierungsbeispiel

0
Jd Patel