Download the PHP package weldist/spatie-medialibrary-uuid-path-generator without Composer

On this page you can find all versions of the php package weldist/spatie-medialibrary-uuid-path-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package spatie-medialibrary-uuid-path-generator

weldist/spatie-medialibrary-uuid-path-generator

Tests PHP Laravel

A UUID-based path generator for spatie/laravel-medialibrary.

A weld.ist project.

Unofficial plugin. Not affiliated with Spatie.

The Problem

spatie/laravel-medialibrary's default DefaultPathGenerator stores media files in a flat structure based on the primary key (ID):

This works fine for small applications, but causes serious issues as the number of media files grows:

The Solution

This package distributes files by turning the leading characters of each media UUID into a sharded directory hierarchy. You pick the shard depth that fits your catalog size:

Conversions and responsive images are placed in dedicated subdirectories under the UUID folder:

Benefits:

Picking a shard depth

The package ships four path generators. They share the same layout — xx/.../xx/<uuid>/ — and differ only in how many two-character shard levels they prepend. Pick the smallest depth that still keeps leaf directories under control for your catalog size:

Generator Layout Max leaf directories Suited to
UuidLevel1PathGenerator xx/<uuid>/ 256 Small catalogs (≲ 250 k files)
UuidLevel2PathGenerator xx/xx/<uuid>/ 65 536 Medium catalogs (low millions) recommended default
UuidLevel3PathGenerator xx/xx/xx/<uuid>/ ~16.7 M Large catalogs / busy object stores
UuidLevel4PathGenerator xx/xx/xx/xx/<uuid>/ ~4.3 B Very large pools or remote disks where flat LIST is expensive

When in doubt, start with Level2 — it handles up to ~10 million files comfortably and keeps cascade cleanup, LIST traversal, and path readability all in a sensible range. Migrating to a deeper layout later is cheaper than overshooting now and dragging around millions of mostly-empty intermediate directories.

Requirements

Installation

Setup

Publish the spatie/laravel-medialibrary config (if you haven't already) and set the path_generator option:

In config/media-library.php, pick the depth you want and wire up the matching path generator together with the cascade-aware file remover:

Swap UuidLevel2PathGenerator for UuidLevel1PathGenerator, UuidLevel3PathGenerator, or UuidLevel4PathGenerator if you need a different shard depth. The file remover and the artisan commands shipped with this package introspect this config and automatically follow the depth you chose — there is no separate setting to keep in sync.

Why UuidFileRemover? The default file remover deletes the UUID directory but leaves the empty shard parent directories (55/0e/84/00/) behind. UuidFileRemover cascades upward and removes each shard level when it becomes empty.

Migrating from DefaultPathGenerator

If your project already has media files stored with spatie's default ID-based structure (1/photo.jpg, 2/photo.jpg), you can migrate them to the UUID path structure.

1. Switch media-library.path_generator to one of this package's UUID generators (pick the depth that suits your catalog — see Picking a shard depth):

The migration command reads this config to decide where files land and refuses to run unless it points to a UUID generator. Switching file_remover_class to UuidFileRemover is optional at this stage — it only affects future deletions (cascade-cleanup of empty shard parents) and can be flipped any time.

2. Run the migration:

It moves all files (including conversions and responsive images), deletes the old ID directories, and is safe to re-run — it skips media whose old directory no longer exists.

Heads up — read window during migration. As soon as Step 1 lands, the application starts resolving media URLs through the UUID generator while the actual files still sit at their ID-based paths. Any read between Step 1 and Step 2 will 404. Schedule the two steps back-to-back during low traffic, or use --dry-run first to estimate the move duration.

Options:

Option Description
disk Disk to migrate (defaults to media-library.disk_name config)
--dry-run Preview what would be moved without touching any files
--force Skip the production confirmation prompt

Reverting to DefaultPathGenerator

If you need to undo the migration and put files back under spatie's ID-based layout, run the inverse command before switching the config away from the UUID generator:

1. With media-library.path_generator still pointing at a UUID generator, run:

It moves each media's main file, conversions, and responsive images from the UUID shard path back to the ID-based path, then cascade-cleans empty shard parents.

2. Switch media-library.path_generator back to Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator (and file_remover_class back to Spatie\MediaLibrary\Support\FileRemover\DefaultFileRemover if you swapped that earlier).

Heads up — read window during reverse migration. Symmetric to the forward migration: while files are being moved, URLs still resolved through the UUID generator (because the config hasn't flipped yet) keep working, but as soon as you flip the config in Step 2 any URL not yet served from the new ID path returns the new layout. Schedule the two steps back-to-back, ideally during low traffic.

Options:

Option Description
disk Disk to migrate (defaults to media-library.disk_name config)
--dry-run Preview what would be moved without touching any files
--force Skip the production confirmation prompt

Cleaning Orphaned Directories

spatie's built-in media-library:clean command identifies orphaned directories using an is_numeric() check, which only works for the default ID-based path structure. This package ships a UUID-aware replacement:

Options:

Option Description
disk Disk to clean (defaults to media-library.disk_name config)
--shard= Limit the scan to the given first-level shards (e.g. --shard=55 --shard=ab). Repeatable. Useful for splitting a large sweep into chunks or rerunning a partial scan on a remote disk. Defaults to every shard (00..ff).
--dry-run List orphaned directories without deleting them
--force Skip the production confirmation prompt

Testing

License

This package is open-sourced software licensed under the MIT license.


All versions of spatie-medialibrary-uuid-path-generator with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
spatie/laravel-medialibrary Version ^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package weldist/spatie-medialibrary-uuid-path-generator contains the following files

Loading the files please wait ...