[英]Spatie Medialibrary Custom Custom Directory As Prefix
我正在使用spatie/laravel-medialibrary:8.0
。
当我上传文件时,它保存在storage/public
目录中。
1/filename.file_extension, 2/filename.file_extension...
我认为1, 2...
是文件的 ID。
有没有办法根据用户放置不同的前缀目录?
例如,我想这样放。
public/storage/user_id/1/filename.file_extension,
我搜索了它,但找不到确切的答案,
谁能帮我?
谢谢
go 的方式在 docs Using a custom directory structure下。
要覆盖默认文件夹结构,可以将符合PathGenerator
的 class 指定为配置文件中的path_generator
。
例如,您可以创建一个新的 class 来扩展该接口并为您的模型返回一些路径
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/';
}
}
然后更新配置文件并指向 class:
'path_generator' => CustomPathGenerator::class,
参考:
spatie/laravel-medialibrary Docs 使用自定义目录结构。
Laracasts Spatie MediaLibrary 默认存储取决于 model “最佳答案”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.