By default, files will be stored inside a directory that uses the id of its Media-object as a name. Converted images will be stored inside a directory named conversions.
Putting files inside their own folders guarantees that files with the same name can be added without any problems.
To override this default folder structure, a class that conforms to the PathGenerator-interface can be specified as the path_generator in the config file. The given class will be loaded through the Laravel Service Container, so feel free to type-hint any dependencies in the constructor.
Let's take a look at the interface:
namespaceSpatie\MediaLibrary\Support\PathGenerator;
useSpatie\MediaLibrary\MediaCollections\Models\Media;
interfacePathGenerator
{
/*
* Get the path for the given media, relative to the root storage path.
*/publicfunctiongetPath(Media $media): string;
/*
* Get the path for conversions of the given media, relative to the root storage path.
*/publicfunctiongetPathForConversions(Media $media): string;
/*
* Get the path for responsive images of the given media, relative to the root storage path.
*/publicfunctiongetPathForResponsiveImages(Media $media): string;
}
There aren't any restrictions on how the directories can be named. When a Media-object gets deleted the package will delete its entire associated directory. To avoid tears or worse, make sure that every media gets stored its own unique directory.
In addition to setting a global path generator in the config file, You can also define a CustomPathGenerator class for specific models directly inside the model's booting() method or within a service provider: