![](/img/trans.png)
[英]Symfony2 + SonataAdmin + VichUploaderBundle - Image Upload Field
[英]Symfony3 + VichUploaderBundle + custom upload directory
我已经用自定义文件名设置了 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;
}
感谢您的时间和知识。
不要在命名器中使用会话,因为命名器必须是无状态的。 要自定义目录,您可以使用目录名称。 请参阅https://github.com/dustin10/VichUploaderBundle/blob/master/docs/namers.md#directory-namer 上的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.