2016-04-25 37 views
0

我有一個自定義文件命名器設置VichUploaderBundle,它工作正常..Symfony3 + 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」 ,並在namer中檢索所述路徑。

它保存到數據庫(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; 
} 

感謝您的時間和知識。

回答