首页 > 解决方案 > 如何获取图像的原始名称?

问题描述

大家好如何获取集合中图像的原始客户端名称?

我有这个新代码可以上传三个以上的图像

public function store(){

        $action = '';

        $data =  $this->validate([
            'album_title' => 'required',
            'photos.*' => 'image|max:2000', // 2MB Max
        ]);

        //New Code with collections
        $filenames = collect($this->photos)->map->store('photos');

        if($this->galleryId){
            Gallery::find($this->galleryId)->update($data);
            $action = 'edit';
        }else{
            // Gallery::create($data);
            Gallery::create([
                'album_title' => $this->album_title,
                'photos' => $filenames->implode(','),
              ]);
            $action = 'store';
        }
        $this->emit('showEmitedFlashMessage', $action);
        $this->resetInputFields();
        $this->emit('refreshParent');
        $this->emit('closeGalleryModal');

    }

但它保存了这样的东西

photos/xTNV0cQ7YzpyUsJ6OdrpQirwfCPdzDtKJAKoWUFm.jpg,photos/32PkRR9bFhM7BqAy9OWG05m5fe53PUXtidEMhlnn.jpg,photos/Dyps8wCsga0zdALGsKEOS9jcYSNshnixw4UWzhEL.jpg

我想要的是保存图像的原始名称。

标签: laravel-8laravel-livewire

解决方案


我通常做的是将文件名设置为 uuid 以存储在系统中,但将原始文件名保留在数据库中,以便您可以根据需要重命名以进行下载。要获取原始文件名,您需要查看帖子数据。

像这样:

$this->active_document->original_filename = $this->uploaded_file->getClientOriginalName();

文档中的更多信息:https ://laravel.com/docs/8.x/filesystem#file-uploads


推荐阅读