【问题标题】:Customer Image Profile客户形象资料
【发布时间】:2017-04-12 14:07:57
【问题描述】:
【问题讨论】:
标签:
symfony
sylius
vichuploaderbundle
【解决方案1】:
以唯一的名称将它们保存在您的文件夹中,并将名称存储在数据库中。
要上传图像,您可以在表单中使用 FileType 字段,记录在 here。我记得您可以使用 ImageType 进行表单验证,这里有一些代码将文件保存在文件夹中并将文件名保存在数据库中:
$user = new User();
$form = $this->createForm(UserForm::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$img = $user->getPhoto();
$fileName = md5(uniqid()).'.'.$img->guessExtension();
$user->setPhoto($fileName);
// Move the file to the directory where brochures are stored
$img->move(
$this->getParameter('user_image_directory'),
$fileName
);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
当你想显示 img 时,你以类似的方式在 twig 中显示它,首先你使用 $user->getPhoto() 读取文件名并将其发送到你的模板,当我调用 user.模板中的getPhoto(),也许就我一个人:
img src="{{ asset('media/users/') }}{{ filename }}"></div>