Laravel 5.3
に複数のファイルをアップロードするにはどうすればよいですか。 1つの画像で試してみると動作しますが、複数の画像はアップロードされません。
これは私のコードです:
if($request->hasFile('attachment'))
{
foreach ($request->allFiles('attachments') as $file) {
$file->store('users/' . $user->id . '/messages');
}
}
これは次のようになりました。
$files = $request->file('attachment');
if($request->hasFile('attachment'))
{
foreach ($files as $file) {
$file->store('users/' . $this->user->id . '/messages');
}
}
name
属性の値の後に[]
を追加する必要がありました。
<input type="file" name="attachment[]" multiple>
次のようなものを試してください:
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 ++;
}
}
}
ブレードを引き続き使用する場合は、これを使用できます。
{{ Form::open(array('url' => 'upload', 'files'=>true)); }}
{{ Form::file('gallery[]', array('multiple'=>true,'accept'=>'image/*')); }}
{{ Form::submit(); }}
{{ Form::close(); }}
そしてコントローラーで:
files = $request->file('gallery');
if($request->hasFile('gallery'))
{
foreach ($files as $file) {
// $file->store('users/' . $this->user->id . '/messages');
//dump($file);
}
}
これをよりシンプルなアプローチで解決しました。入力ファイルの種類がこのような<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++;
}
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;
}
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');
}
ビューファイルの作成
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>