我正在使用spatie/laravel-medialibrary:8.0。
当我上传文件时,它被保存在storage/public目录中。
1/filename.file_extension, 2/filename.file_extension...我认为1, 2...是文件的ids。
有没有办法根据用户设置不同的前缀目录?
例如,我想把它写成这样。
public/storage/user_id/1/filename.file_extension, 我搜索了一下,但找不到确切的答案,
有谁可以帮我?
谢谢
发布于 2020-05-26 17:28:47
它的方法是在docs 使用自定义目录结构下。
若要重写默认文件夹结构,可以将符合PathGenerator-interface的类指定为配置文件中的path_generator。
例如,您可以创建一个扩展该接口的新类,并为模型返回一些路径。
class CustomPathGenerator implements PathGenerator
{
public function getPath(Media $media) : string
{
if ($media instanceof Post) {
return 'user_id/' . $media->user_id . '/' . $media->id;
}
return $media->id;
}
public function getPathForConversions(Media $media) : string
{
return $this->getPath($media) . 'conversions/';
}
public function getPathForResponsiveImages(Media $media): string
{
return $this->getPath($media) . 'responsive/';
}
}然后更新配置文件并指向该类:
'path_generator' => CustomPathGenerator::class,参考文献:
空间/拉拉-介质图书馆 Docs 使用自定义目录结构.
空间MediaLibrary的默认存储取决于模型“最佳答案”。
https://stackoverflow.com/questions/62026654
复制相似问题