繁体   English   中英

如何保存不同名称的图像?

[英]How to save images with different names?

我正在处理有关多张图片上传的一些问题。 我的项目处理用户输入多个图像并保存到数据库的表单,但首先将图像临时存储在目录中以显示各种预览。 在我的 class 中,它处理图像的上传,我试图重命名图像,如下所示。

我在下拉列表中上传了七张图片。

落下

我看到它的所有图像都被一个存储在数据库中。

桌子

虽然它必须以不同的名称存储。

Controller

public function store(Product $product, Request $request)
{
    $path = null;
    if ($request->hasFile('file'))
    {
        $file = $request->file('file');
        $name = time();
        $extension = $file->getClientOriginalExtension();
        $fileName = $name . '.' . $extension;
        $path = $file->storeAs($this->path, $fileName, 'public');
    }
    $gallery = Gallery::create([
        'product_id' => $product->id,
        'image' => $path,
    ]);
    return response()->json($gallery);
}
    $file = $request->file('file');
    $name = date('YmdHi');
    $extension = $name.$file->getClientOriginalExtension();

controller中,替换即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM