douzhi3776 2016-10-04 13:19
浏览 58
已采纳

Zend Form:将图片移动到文件夹中

I'm working on a project with ZF2 and Zend Form. I'd like to add an avatar into a user profile.

The problem is that I only get the file name and save it in the DB. I would like to insert it into a folder so I'll be able to get it and display it. The rest of the form is working.

My guess is that I have to get information from $FILES, but I have no idea how to do this. I've read the documentation but can't see how to apply this to my project.

Thank you in advance!

Here's my Controller Action

public function signinAction()
    {
        $this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');

        $form = new SignupForm($this->em);

        $model = new ViewModel(array("form" => $form));

        $url = $this->url()->fromRoute("signin");
        $prg = $this->prg($url, true);

        if($prg instanceof \Zend\Http\PhpEnvironment\Response){
           return $prg;
        }
        else if($prg === false){
           return $model;
        }

        $user = new User();
        $form->bind($user) ;
        $form->setData($prg) ;

        if($form->isValid()){
            $bcrypt = new Bcrypt() ;
            $pwd = $bcrypt->create($user->getPassword());
            $user->setPassword($pwd);
            $this->em->persist($user) ;
            $this->em->flush() ;

            return $this->redirect()->toRoute('login');
        }

        return $model ;
    }

Here's my form file :

class SignupForm extends Form 
{
    private $em = null;

    public function __construct($em = null) {
        $this->em = $em;
        parent::__construct('frm-signup');

        $this->setAttribute('method', 'post');
        $this->setHydrator(new DoctrineEntity($this->em, 'Application\Entity\User'));

        //Other fields
         ...

        //File
        $this->add(array(
            'type' => "File",
            'name' => 'avatar',
            'attributes' => array(
                'value' => 'Avatar',
            ),     
        ));    

        //Submit
         ...         
    }   
}

And the form in my view :

        $form = $this->form;         
         echo $this->form()->openTag($form);
         //other formRow
         echo $this->formFile($form->get('avatar')); 
         echo $this->formSubmit($form->get('submit'));
         echo $this->form()->closeTag();
  • 写回答

1条回答 默认 最新

  • duanpen9294 2016-10-06 09:04
    关注

    There are two things you could look at for getting your avatar to work:

    1. Using the Gravatar view helper (uses gravatar.com service that automatically links images to email addresses)
      • documentation on using the gravatar service can be found here
    2. Upload images yourself with the file upload classes that are shipped with ZF2:
      • form class for file upload can be found here
      • input filter class documentation can be found here

    If you follow those docs you should be able to manage what you want.


    Note: check especially the use of the Zend\Filter\File\RenameUpload filter in the example in the input filter documentation. This filter renames/moves the uploaded avatar file to the desired location.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信