Comment puis-je télécharger plusieurs fichiers dans Laravel 5.3
. Si je l'essaie avec 1 image, cela fonctionne mais plusieurs images ne sont pas téléchargées.
Ceci est mon code:
if($request->hasFile('attachment'))
{
foreach ($request->allFiles('attachments') as $file) {
$file->store('users/' . $user->id . '/messages');
}
}
Cela fonctionne maintenant comme ceci:
$files = $request->file('attachment');
if($request->hasFile('attachment'))
{
foreach ($files as $file) {
$file->store('users/' . $this->user->id . '/messages');
}
}
J'ai dû ajouter []
après la valeur de l'attribut name
, donc:
<input type="file" name="attachment[]" multiple>
Essayez quelque chose comme ça:
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 ++;
}
}
}
Si vous voulez toujours travailler avec blade, vous pouvez utiliser ceci:
{{ Form::open(array('url' => 'upload', 'files'=>true)); }}
{{ Form::file('gallery[]', array('multiple'=>true,'accept'=>'image/*')); }}
{{ Form::submit(); }}
{{ Form::close(); }}
Et dans votre contrôleur:
files = $request->file('gallery');
if($request->hasFile('gallery'))
{
foreach ($files as $file) {
// $file->store('users/' . $this->user->id . '/messages');
//dump($file);
}
}
app/Http/Controllers/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');
}
Créer un fichier de vue
ressources/vues/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 - Exemple de téléchargement de plusieurs fichiers avec validation