简体   繁体   English

Symfony3 + VichUploaderBundle + 自定义上传目录

[英]Symfony3 + VichUploaderBundle + custom upload directory

I have setup VichUploaderBundle with custom file namer and it works fine..我已经用自定义文件名设置了 VichUploaderBundle,它工作正常..

my relevant config.yml:我的相关 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.我将所需的上传路径保存到控制器中的会话变量“upload_files_path”,并在命名器中检索所述路径。

It saves to database (id, image_name, udated_at), but does not write file to filesystem!它保存到数据库(id、image_name、udated_at),但不将文件写入文件系统!

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.这是我对自定义文件路径的配置:所以我将“uri_prefix”和“upload_destination”编辑为空白。 edited config.yml已编辑的 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请参阅https://github.com/dustin10/VichUploaderBundle/blob/master/docs/namers.md#directory-namer 上的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM