I have setup VichUploaderBundle with custom file namer and it works fine..
my relevant config.yml:
vich_uploader:
db_driver: orm
mappings:
product_image:
uri_prefix: '/uploads/products'
upload_destination: '%kernel.root_dir%/../web/uploads/products'
namer: namer.product_image
#namer: vich_uploader.namer_uniqid
#namer: vich_uploader.namer_origname
#namer: vich_uploader.namer_property
inject_on_load: false
delete_on_update: true
delete_on_remove: true
my custom namer:
public function name($obj, PropertyMapping $mapping)
{
$file = $mapping->getFile($obj);
$new_name = $this->generateRandomSecret();
if ($extension = $file->guessExtension())
{
$new_name = $new_name .'.'. $extension;
}
return $new_name;
}
However, i want to use custom path to upload file.
I save needed upload path to session variable "upload_files_path" in controller and retrieve said path in the namer.
It saves to database (id, image_name, udated_at), but does not write file to filesystem!
When i call
<img src="{{ vich_uploader_asset(product, 'imageFile') }}" />
in the template it returns file path prepended with "/". I can not figure out how to make it work.
Here is my configuration for custom file path: So i edited "uri_prefix" and "upload_destination" to be blank. edited config.yml
vich_uploader:
db_driver: orm
mappings:
product_image:
uri_prefix: ''
upload_destination: ''
namer: namer.product_image
inject_on_load: false
delete_on_update: true
delete_on_remove: true
my updated custom namer: Here i concatenate upload path with new file name.
public function name($obj, PropertyMapping $mapping)
{
$file = $mapping->getFile($obj);
$new_name = $this->generateRandomSecret();
if ($extension = $file->guessExtension())
{
$new_name = $new_name .'.'. $extension;
}
$upload_path = $this->container->get('session')->get('upload_files_path');
$full_path = $upload_path . $new_name;
return $full_path;
}
Thanks for your time and knowledge.
Don't use sessions inside your namer, since namer must be stateless. To customize directory you can use a directory namer. see docs at https://github.com/dustin10/VichUploaderBundle/blob/master/docs/namers.md#directory-namer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.