Getting a file path and URL

The path and filename of a stored file in the storage backend that was used to store the file is generated by a path builder. The event listener that stored your file has used a path builder to generate the path based on the entity data. This means that if you have the entity and instantiate a path builder you can build the path to it in any place.

The plugin already provides you with several convenience short cuts to do that.

Be aware that whenever you use a path builder somewhere, you must use the same path builder and options as when the entity was created. They're usually the same as configured in your event listener.

Getting it from an entity

If you're using an entity from this plugin, or extending it they'll implement the PathBuilderTrait. This enables you to set and get the path builder on the entities.

Due to some limitations of the CakePHP core you can't pass options to the entity when calling Table::newEntity(). As a workaround for that you'll have to set it manually:

$entity->pathBuilder('PathBuilderName', ['options-array' => 'goes-here']);
$entity->path(); // Gets you the path in the used storage backend to the file
$entity->url(); // Gets you the URL to the file if possible

Getting it using the storage helper

The storage helper is basically just a proxy to a path builder. The helper takes two configuration options:

  • pathBuilder: Name of the path builder to use.
  • pathBuilderOptions: The options passed to the path builders constructor.

Make sure that the options you pass and the path builder are the same you've used when you uploaded the file! Otherwise you end up with a different path!

// Load the helper
$this->loadHelper('Burzum/FileStorage.Storage', [
    'pathBuilder' => 'Base',
    // The builder options must match the options and builder class that were used to store the file!
    'pathBuilderOptions' => [
        'modelFolder' => true,
    ]
]);

// Use it in your views
$url = $this->Storage->url($yourEntity);

// Change the path builder at run time
// Be carefully, this will change the path builder instance in the helper!
$this->Storage->pathBuilder('SomePathBuilder', ['options' => 'here']);