我知道VichUpload和名字,但我必须面对两个不同的文件上传,我需要特殊的命名约定,我无法使用当前的VichUpload文档。
第一个文件上传是针对名为“School”的实体,该实体管理每个学校及其徽标。因此,以网络根目录'../web/files/schools/'为例,然后我将带有上传文件名的school_id和'logo'文件夹,因此它可能是'../web/files/学校/ 000001 / logo.png”。
然后,第二个实体是“学生”来存储照片,并带有来自学校实体的school_id外键。因此,生成的文件名将取决于学校ID和学生ID,作为学生的根源.. ..web/files/schools/<school_id>/students/<student_id>.[jpg|png] ',让student_id在左边有六个零填充。
这是config.yml
中有关此内容的部分(更新信息):
school_image:
upload_destination: "../web/files/images/schools"
uri_prefix: "/files/images/schools"
directory_namer:
service: vich_uploader.directory_namer_subdir
options: { property: 'schoolDirectory', chars_per_dir: 6, dirs: 1 }
namer:
service: vich_uploader.namer_property
options: { property: 'idSlug' }
inject_on_load: true
delete_on_update: true
delete_on_remove: true
student_image:
upload_destination: "../web/files/images/schools"
uri_prefix: "/files/images/schools"
directory_namer:
service: vich_uploader.directory_namer_subdir
options: { property: 'userDirectory', chars_per_dir: 6, dirs: 1 }
namer:
service: vich_uploader.namer_property
options: { property: 'userImage'}
inject_on_load: true
delete_on_update: true
delete_on_remove: true
在学校实体中(可能使用肮脏的解决方法):
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="school_image", fileNameProperty="imageName")
*
* @Assert\Image()
*
* @var File
*/
private $imageFile;
public function getIdSlug()
{
$slug = sprintf("%06d", $this->getId());
return $slug;
}
public function getSchoolDirectory()
{
$slug = sprintf("%06d", $this->getId());
return $slug;
}
在学生实体中(不工作,如下所述):
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="student_image", fileNameProperty="imageName")
*
* @Assert\Image()
*
* @var File
*/
private $imageFile;
public function getUserDirectory()
{
$schoolDir = $this->getSchool()->getSchoolDirectory();
$dir = $schoolDir.'/students/'.sprintf("%06d", $this->getId());
return $dir;
}
public function getUserImage()
{
return $this->getUsername() . $this->getImageFile()->getExtension();
}
这个带有“namer”和“directory_namer”的设置似乎忽略了directory_namer中的路径,并使用路径“&lt; namer&gt; /&lt; namer&gt; .ext”而不是“&lt; directory_namer&gt; /&lt; namer&gt; .EXT”。如果我更改了getUserImage()函数并添加了getUserDirectory()的结果(即,db存储“&lt; directory_namer&gt; /&lt; namer&gt; .ext”而不仅仅是“&lt; namer&gt; .ext”),则“ &LT; directory_namer&gt;”中路径被忽略,只创建“&lt; namer&gt; .ext”。
由于upload_prefix和uri_destination似乎没有处理变量数据,我如何为这两种情况设置一个名称或其他任何方法来获取此路径?
BTW,我正在使用Symfony 3.1,并且根据vendor
文件夹中捆绑的composer.json,作曲家不允许更新超出“1.7.x-dev”的vich。 如果您发现它应该可以使用此设置并且可能的解决方案是升级,我将感谢指出解决问题的特定文件,因此我可以手动粘贴任何错误。 < / p>
SOLUTION:
问题是,由于“旧”版本,缺少directory_namer类,PropertyDirectoryNamer,vich_uploader.namer_directory_property
servicename,而不是vich_uploader.directory_namer_subdir
,这是一个错误的类。我复制了这个类并将其注册在namer.xml文件中,然后我得到了预期的结果。现在我要尝试将其标记为已解决。
答案 0 :(得分:2)
您需要创建实现
的自定义目录名称类Vich\UploaderBundle\Naming\DirectoryNamerInterface
例如:
namespace App\Services;
use Vich\UploaderBundle\Mapping\PropertyMapping;
class SchoolDirectoryNamer implements DirectoryNamerInterface
{
public function directoryName($object, PropertyMapping $mapping): string
{
// do what you want with $object and $mapping
$dir = '';
return $dir;
}
}
然后,将其作为服务。
services:
# ...
app.directory_namer.school:
class: App\Services\SchoolDirectoryNamer
最后一步是config vich_uploader
vich_uploader:
# ...
mappings:
school:
upload_destination: school_image
directory_namer: app.directory_namer.school
student:
upload_destination: student_image
directory_namer: app.directory_namer.student
来源:VichUploaderBundle - How To Create a Custom Directory Namer