简体   繁体   English

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

[英]How to save images with different names?

I'm dealing with a few issues regarding multiple images uploads.我正在处理有关多张图片上传的一些问题。 My project deals with a form the user inputs multiple images that save to a database, but first temporarily stores the images in a directory to show a preview of sorts.我的项目处理用户输入多个图像并保存到数据库的表单,但首先将图像临时存储在目录中以显示各种预览。 In my class, which deals with the uploading of the image I am trying to rename the images as seen below.在我的 class 中,它处理图像的上传,我试图重命名图像,如下所示。

I uploaded seven images on the dropdown.我在下拉列表中上传了七张图片。

落下

I see that all its images were stored in the database by one.我看到它的所有图像都被一个存储在数据库中。

桌子

While it had to be stored under different names.虽然它必须以不同的名称存储。

Controller 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();

In controller, just replace it. controller中,替换即可。

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

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