【问题标题】:I want to upload a profile picture with symfony2 and doctrine我想用 symfony2 和教义上传个人资料图片
【发布时间】:2015-10-29 13:48:55
【问题描述】:

在 User.php(实体名称为 User)中,我在 User 实体中有一个字段,名为 userPic ,输入 String

在 UserType.php 文件中,我提到了userPic,如下所示:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('userFullname')
        ->add('userName')
        ->add('userEmail')
        ->add('userPassword')
        ->add('userPic', 'file', array ('label'=>'profile Picture'))
        ->add('gender','choice',array('choices' => array('m' => 'Male', 'f' => 'Female')))

        ->add('isActive')
    ;
}

现在在控制器中,我得到了如下所示的表单字段

/**
 * Creates a new User entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new User();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
    }

    return $this->render('MWANRegisterBundle:User:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

我必须在哪里提供我想要保存图片的路径?如何将上传的文件保存在我想要的目录中,并将目录路径保存在数据库中?

【问题讨论】:

标签: php symfony orm doctrine-orm doctrine


【解决方案1】:

Christian 的回答是有效的,但我只想更具体地指出如何做被问到的事情。只需这样做:

if ($form->isValid()) {
    $file = $form->getData()['file'];
    $file->move('/your/path/to/your/file', 'yourFileName');
    // Do the rest
    ...
}

希望这会有所帮助。

【讨论】:

  • 伙计们,我有一个问题,请相应地指导我,我将不胜感激。问题与 FOS-user-Bundle 有关,我希望将角色嵌入到我在 RegisterType 中创建的自定义表单中。我已经从基本实体扩展了我的用户实体
  • 对于新问题,请随意创建一个新问题,因为 cmets 不适用于此用途。此外,它会比在评论中获得更好的可见性。
  • 感谢您的告知,因为我在这里的声誉太低,这就是为什么 Stakeoverflow 不允许我提出很多问题。
【解决方案2】:

您需要在您的实体中创建上传方法。查看此链接了解更多详情http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

public function uploadFile()
{
    // the file property can be empty if the field is not required
    if (null === $this->getFile()) {
        return;
    }

    // use the original file name here but you should
    // sanitize it at least to avoid any security issues

    // move takes the target directory and then the
    // target filename to move to
    $this->getFile()->move($this->getUploadDir(), $this->getFile()->getClientOriginalName());

    // set the path property to the filename where you've saved the file
    $this->path = $this->getFile()->getClientOriginalName();

    // clean up the file property as you won't need it anymore
    $this->file = null;
}

/**
 * Creates a new User entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new User();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        // Upload file
        $entity->uploadFile();    

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
    }

    return $this->render('MWANRegisterBundle:User:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 2020-01-10
    • 2014-04-14
    • 1970-01-01
    • 2015-05-22
    • 2016-06-30
    • 1970-01-01
    • 2020-11-02
    相关资源
    最近更新 更多