我想上传多个图像并将其存储到数据库中。
这是我的代码:
<form method="POST" action="{{ route('validated', ['id' => $product->id]) }}) }}">
@csrf
<input type="hidden" name="ID" name="ID" value="{{ $product->id }}">
<div class="form-group row">
<label for="Image" class="col-sm-2 col-form-label">Image</label>
<img src="{{ $product->imageURL }}" />
</div>
<div class="form-group row">
<div class="col-sm-12">
<button type="submit" class="btn btn-lg btn-success">Valider les données</button> | <a href="#">Relancer le fournisseur</a>
</div>
</div>
</form>
并进入我的控制器功能:
$imagePath = Storage::putFile('uploads/image/', $request->image, "imagename");
我有这个错误:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError抛出消息“调用字符串上的成员函数hashName()”
Stacktrace:0:Symfony \ Component \ Debug \ Exception \ FatalThrowableError在C:\ laragon \ www \ MyProject \ vendor \ laravel \ framework \ src \ Illuminate \ Filesystem \ FilesystemAdapter.php:208
您好我认为您需要使用putFileAs方法来自定义名称,例如:
//Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
此方法接受Illuminate \ Http \ File或Illuminate \ Http \ UploadedFile实例,并自动将文件流式传输到所需位置:
对于多个图片上传,您可以尝试以下代码:
$images = $request->file('images');
foreach ($images as $key => $image) {
if ($request->hasFile('images') && $request->file('images')[$key]->isValid()) {
$urlPath = $request->images[$key]->store('public/images');
$image = new Images();
$image->path = $urlPath;
$image->save();
} else {
return redirect()->back()->withInput()->with('errors', 'Invalid Image file found!');
}
}
假设您必须在表单中添加enctype="multipart/form-data"
,在函数参数中添加Request $request
,并在公共目录中添加storage link
。
我希望它会有所帮助。谢谢
首先,如果要上传图像,您的表单必须具有enctype="multipart/form-data"
属性。
基于Laravel documentation你可以使用这样的东西:
$path = $request->file('image')->store('image');
如果您使用的是Laravel,则应该能够执行以下操作:
$imagePath = $request->file('image')->store('uploads/image');
或者,如果要指定文件名,可以执行以下操作:
$imagePath = $request->file('image')->storeAs('uploads/image', 'myfilename.jpg');
更新
我注意到在HTML表单中没有文件上传输入。如果您没有,Laravel将没有要存储的文件。
如果要编写可重用代码并以不同方式使用它,请执行此操作
例如,我们在Image模型中有Post和Image模型添加此代码
public function post()
{
return $this->belongsTo(Post::class);
}
public static function named($name,$type)
{
$photo = new static;
$photo->saveAs($name,$type);
return $photo;
}
public function saveAs($name,$type)
{
$this->path = sprintf("%s/%s/%s-%s",'baseDir',$type,time(), $name);
$this->name = sprintf("%s-%s", time(), $name);
$this->type = $type;
}
public function moves(UploadedFile $file,$type)
{
$file->move($this->baseDir.'/'.$type, $this->name);
return $this;
}
并在你想要的模型中为Post添加一个图像:
public function images()
{
return $this->hasMany(Image::class);
}
public function addImages(Image $image)
{
return $this->images()->save($image);
}
在控制器中:
public function addImages(ImageRequest $request,$id)
{
$image = $this->makeImage($request->file('file'),$request->type);
Post::where('id' ,'=', $id)->first()->addImage($image);
return back();
}
protected function makeImage(UploadedFile $file,$type)
{
return Image::named($file->getClientOriginalName(),$type)->moves($file,$type);
}