现在我使用一种简单的方式上传图片:
if ($request->hasFile("images")) {
$file = $request->file("images");
// Do uploading to Storage
$uploaded = Storage::put($destinationPath. $fileName, file_get_contents($file->getRealPath()));
}
如果我有以HTML格式提交的images[]
,我该如何上传多个文件?
是否可以使用Storage::put()
?
答案 0 :(得分:2)
在视图中(使用LaravelCollective包):
{{ Form::open(['action' => 'MyController@store', 'class' => 'form-horizontal', 'files' => true, 'enctype' => 'multipart/form-data' ]) }}
{{ Form::file('attachments[]', ['class' => 'form-control', 'roles' => 'form', 'multiple' => 'multiple']) }}
{{ Form::close() }}
在控制器中:
public function store(Request $request)
{
if (($request->has('attachments'))) {
$files = $request->file('attachments');
$destinationPath = storage_path() . '/app/public/';
foreach ($files as $file) {
$fileName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$storeName = $fileName . '.' . $extension;
// Store the file in the disk
$file->move($destinationPath, $storeName);
}
}
}
答案 1 :(得分:2)
如果您的表单在images []数组下提交多个文件,您可以相应地循环它们。
如果你发布了html格式,也会有所帮助。
<?php
$files = $request->file("images");
$uploaded = [];
if($files){
foreach($files as $file) {
$uploaded[] = Storage::put($destinationPath. $fileName, file_get_contents($file->getRealPath()));
}
}
});