我已经使用自定义文件命名器设置了 VichUploaderBundle,它工作正常..
我的相关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
我的自定义命名器:
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;
}
但是,我想使用自定义路径来上传文件。
我将所需的上传路径保存到控制器中的会话变量“upload_files_path”,并在命名器中检索所述路径。
它保存到数据库(id、image_name、udated_at),但不将文件写入文件系统!
当我打电话时
<img src="{{ vich_uploader_asset(product, 'imageFile') }}" />
在模板中,它返回以“/”开头的文件路径。我不知道如何使它工作。
这是我对自定义文件路径的配置:所以我将“uri_prefix”和“upload_destination”编辑为空白。编辑 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
我更新的自定义命名器:在这里我将上传路径与新文件名连接起来。
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;
}
感谢您的时间和知识。